class EmittingStream(QtCore.QObject): textWritten = QtCore.pyqtSignal(str) def write(self, text): self.textWritten.emit(str(text))
时间: 2024-02-26 09:53:32 浏览: 94
这是一个 Python 中使用 PyQt 模块的类,它继承自 QObject 类。它定义了一个 textWritten 信号(使用 PyQt 的信号槽机制),并且重写了 write() 方法,将传入的文本转换成字符串并发射 textWritten 信号。这个类的主要作用是将 Python 中的 print 输出重定向到 PyQt 应用程序的窗口中,使得 print 输出可以在应用程序中显示。
相关问题
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_()) 请检查上述代码为何没有画图并展示
这段代码缺少了一些必要的部分,例如 `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_())
```
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, 49) if val < 50: self.queue.append(val) print(self.queue) 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: print("start draw") val = self.queue.pop(0) self.series.append(self.series.count(), val) else: break self.finished.emit() print('end draw') 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_()) 检查上述代码为何在点击start按钮后只生成三个随机数后就停止生成数据,且未画图
在上述代码中,当随机数生成的值大于等于50时,没有将这个值加入到队列中。因此,队列中的随机数数量不足以绘制图表。解决方法是将第11行代码修改为`if val <= 50:`。另外,在 `RandomGenerator` 类的 `start` 方法中,当队列中只有一个随机数时,应该使用 `self.queue[0]` 而不是 `val`。这些修改后,点击 start 按钮后,会绘制出完整的图表。
阅读全文