我在Python方面还是个新手,正试图制作一个PyQt4应用程序,在其中嵌入了PyQtGraph。我有一个非常棒的PyQtGraph实时绘图仪,它能够很好地工作:
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import random
app = QtGui.QApplication([])
p = pg.plot()
curve = p.plot()
data = [0]
def updater():
data.append(random.random())
curve.setData(data) #xdata is not necessary
timer = QtCore.QTimer()
timer.timeout.connect(updater)
timer.start(0)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
为了嵌入到更大的PyQt4应用程序中,我需要一个布局来包含pyqtgraph.PlotWidget()。为此,我在我的MainWindow中设置了一个centralWidget。我创建了一个按钮,当我通过plotter函数调用updater函数时,它应该开始绘图,但是什么也没有发生:
import sys
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import random
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
login_widget = LoginWidget(self)#to say where the button is
login_widget.button.clicked.connect(self.plotter)
self.central_widget.addWidget(login_widget)
def plotter(self):
self.data =[0]
timer = QtCore.QTimer()
timer.timeout.connect(self.updater)
timer.start(0)
def updater(self):
self.data.append(random.random())
plot.setData(self.data)
class LoginWidget(QtGui.QWidget):
def __init__(self, parent=None):
global plot
super(LoginWidget, self).__init__(parent)
layout = QtGui.QHBoxLayout()
self.button = QtGui.QPushButton('Start Plotting')
layout.addWidget(self.button)
plot = pg.PlotWidget()
layout.addWidget(plot)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication([])
window = MainWindow()
window.show()
app.exec_()
因为我需要使用线程,所以什么都没有发生吗?