import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtChart import * class RandomGenerator(QObject): dataReady = pyqtSignal(int) stopSignal = pyqtSignal() def init(self): super().init() self.queue = [] self.count = 0 def start(self): while True: val = random.randint(1, 100) if val < 50: self.queue.append(val) if len(self.queue) == 1: self.dataReady.emit(val) else: self.count += 1 if self.count >= 2: self.stopSignal.emit() break class ChartDrawer(QObject): finished = pyqtSignal() def init(self, queue): super().init() self.queue = queue self.series = QLineSeries() def start(self): while True: if len(self.queue) > 0: val = self.queue.pop(0) self.series.append(self.series.count(), val) else: break self.finished.emit() class MainWindow(QMainWindow): def init(self): super().init() self.generator = RandomGenerator() self.drawer = ChartDrawer(self.generator.queue) self.chartView = QChartView() self.chart = QChart() self.chart.addSeries(self.drawer.series) self.chart.createDefaultAxes() self.chartView.setChart(self.chart) self.startButton = QPushButton("Start") self.startButton.clicked.connect(self.start) self.stopButton = QPushButton("Stop") self.stopButton.clicked.connect(self.stop) self.stopButton.setEnabled(False) self.statusBar().showMessage("Ready") layout = QVBoxLayout() layout.addWidget(self.chartView) layout.addWidget(self.startButton) layout.addWidget(self.stopButton) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) def start(self): self.statusBar().showMessage("Running") self.generatorThread = QThread() self.generator.moveToThread(self.generatorThread) self.generatorThread.started.connect(self.generator.start) self.generator.dataReady.connect(self.handleDataReady) self.generator.stopSignal.connect(self.handleStopSignal) self.generatorThread.start() self.drawerThread = QThread() self.drawer.moveToThread(self.drawerThread) self.drawer.finished.connect(self.handleDrawerFinished) self.drawerThread.start() self.startButton.setEnabled(False) self.stopButton.setEnabled(True) def stop(self): self.statusBar().showMessage("Stopping") self.generator.stopSignal.emit() self.generatorThread.quit() self.drawerThread.quit() self.startButton.setEnabled(True) self.stopButton.setEnabled(False) def handleDataReady(self, val): self.drawer.series.append(self.drawer.series.count(), val) def handleStopSignal(self): self.generatorThread.quit() def handleDrawerFinished(self): self.chartView.update() self.statusBar().showMessage("Finished") self.startButton.setEnabled(True) self.stopButton.setEnabled(False) if name == 'main': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) 请检查上述代码为何没有画图并展示
时间: 2024-03-11 13:06:23 浏览: 90
PyQt5安装教程.zip
这段代码缺少了一些必要的部分,例如 `random` 模块的导入,以及 `RandomGenerator` 类中 `init` 方法的名称错误。此外,`ChartDrawer` 的构造函数应该接受 `queue` 参数并将其存储为实例属性。
另外,`MainWindow` 类中 `start` 方法的最后一行应该是 `self.chartView.show()`,而不是 `self.chartView.update()`。
以下是修改后的代码,应该可以正确地显示图表:
```
import random
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtChart import *
class RandomGenerator(QObject):
dataReady = pyqtSignal(int)
stopSignal = pyqtSignal()
def __init__(self):
super().__init__()
self.queue = []
self.count = 0
def start(self):
while True:
val = random.randint(1, 100)
if val < 50:
self.queue.append(val)
if len(self.queue) == 1:
self.dataReady.emit(val)
else:
self.count += 1
if self.count >= 2:
self.stopSignal.emit()
break
class ChartDrawer(QObject):
finished = pyqtSignal()
def __init__(self, queue):
super().__init__()
self.queue = queue
self.series = QLineSeries()
def start(self):
while True:
if len(self.queue) > 0:
val = self.queue.pop(0)
self.series.append(self.series.count(), val)
else:
break
self.finished.emit()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.generator = RandomGenerator()
self.drawer = ChartDrawer(self.generator.queue)
self.chartView = QChartView()
self.chart = QChart()
self.chart.addSeries(self.drawer.series)
self.chart.createDefaultAxes()
self.chartView.setChart(self.chart)
self.startButton = QPushButton("Start")
self.startButton.clicked.connect(self.start)
self.stopButton = QPushButton("Stop")
self.stopButton.clicked.connect(self.stop)
self.stopButton.setEnabled(False)
self.statusBar().showMessage("Ready")
layout = QVBoxLayout()
layout.addWidget(self.chartView)
layout.addWidget(self.startButton)
layout.addWidget(self.stopButton)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def start(self):
self.statusBar().showMessage("Running")
self.generatorThread = QThread()
self.generator.moveToThread(self.generatorThread)
self.generatorThread.started.connect(self.generator.start)
self.generator.dataReady.connect(self.handleDataReady)
self.generator.stopSignal.connect(self.handleStopSignal)
self.generatorThread.start()
self.drawerThread = QThread()
self.drawer.moveToThread(self.drawerThread)
self.drawer.finished.connect(self.handleDrawerFinished)
self.drawerThread.start()
self.startButton.setEnabled(False)
self.stopButton.setEnabled(True)
def stop(self):
self.statusBar().showMessage("Stopping")
self.generator.stopSignal.emit()
self.generatorThread.quit()
self.drawerThread.quit()
self.startButton.setEnabled(True)
self.stopButton.setEnabled(False)
def handleDataReady(self, val):
self.drawer.series.append(self.drawer.series.count(), val)
def handleStopSignal(self):
self.generatorThread.quit()
def handleDrawerFinished(self):
self.chartView.show()
self.statusBar().showMessage("Finished")
self.startButton.setEnabled(True)
self.stopButton.setEnabled(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
阅读全文