【发布时间】:2023-12-16 18:00:01
【问题描述】:
我是 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。我创建了一个按钮,它应该像前面的代码一样开始绘图,但是当我通过绘图仪函数调用更新器函数时什么也没发生:
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_()
因为我必须穿线,所以什么都没有发生?
【问题讨论】:
标签: python pyqt pyqt4 pyqtgraph