如何解决pyqt5中多窗口多线程更新
我正在尝试使用一个计时器,我可以使用它来使用 pyqt5 更新多个窗口中的值。到目前为止,主窗口打开时带有用于打开其他窗口的按钮,但是当我按下任一按钮时,它就会崩溃。
from PyQt5.QtWidgets import QApplication,QMainWindow,QPushButton,QLabel,QVBoxLayout,QWidget,QLineEdit
from PyQt5.QtCore import QRunnable,pyqtSlot,QTimer,QThreadPool
import sys
timer = QTimer()
timer.setInterval(1000)
timer.start()
class Worker(QRunnable):
def __init__(self,fn,*args,**kwargs):
super(Worker,self).__init__()
self.fn = fn
self.args = args
self.kwargs = kwargs
@pyqtSlot()
def run(self):
self.fn(*self.args,**self.kwargs)
class AnotherWindow(QWidget):
def __init__(self):
super().__init__()
self.x = 0
layout = QVBoxLayout()
self.label = QLabel("Another Window")
layout.addWidget(self.label)
self.line = QLineEdit(self)
layout.addWidget(self.line)
self.setLayout(layout)
timer.timeout.connect(self.update_line)
def update_line(self):
self.x += 1
self.line.setText(str(self.x))
class AnotherWindow2(QWidget):
def __init__(self):
super().__init__()
self.x = 0
layout = QVBoxLayout()
self.label = QLabel("Another Window2")
layout.addWidget(self.label)
self.line = QLineEdit(self)
layout.addWidget(self.line)
self.setLayout(layout)
timer.timeout.connect(self.update_line)
def update_line(self):
self.x += 3
self.line.setText(str(self.x))
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
l = QVBoxLayout()
button1 = QPushButton("Push for Window 1")
button1.clicked.connect(self.show_new_window)
l.addWidget(button1)
button2 = QPushButton("Push for Window 2")
button2.clicked.connect(self.show_new_window2)
l.addWidget(button2)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
self.threadpool = QThreadPool()
print("Multithreading with maximum %d threads" % self.threadpool.maxThreadCount())
def show_new_window(self):
worker = Worker(self.open_AnotherWindow)
self.threadpool.start(worker)
def open_AnotherWindow(self):
self.w = AnotherWindow()
self.w.show()
def show_new_window2(self,checked):
worker2 = Worker(self.open_AnotherWindow2)
self.threadpool.start(worker2)
def open_AnotherWindow2(self,checked):
self.w2 = AnotherWindow2()
self.w2.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
打印出线程数。我也试过把
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.update_line)
self.timer.start()
在每个 AnotherWindow(2) init 函数中,但也会崩溃。也许计时器应该以某种方式在主窗口中?虽然它的结果没有显示在那里。我看过关于多线程和多窗口的教程,但没有看到每个线程都有一个窗口,所有线程都访问相同的数据。
解决方法
为什么一定要使用多线程?线程不是万能的解决方案,相反,如果不知道它们的缺点,那么它们带来的问题多于解决方案。在您的情况下,这是不必要的,因为您没有任何阻塞 GUI 的耗时任务。底线:在必要时使用工具,而不是在需要时使用。
另一方面,您正在从另一个线程创建 GUI,Qt 禁止该线程创建 GUI,因为它不是线程安全的。
class AnotherWindow(QWidget):
def __init__(self):
super().__init__()
self.x = 0
layout = QVBoxLayout()
self.label = QLabel("Another Window")
layout.addWidget(self.label)
self.line = QLineEdit(self)
layout.addWidget(self.line)
self.setLayout(layout)
timer = QTimer(self)
timer.timeout.connect(self.update_line)
timer.start()
def update_line(self):
self.x += 1
self.line.setText(str(self.x))
class AnotherWindow2(QWidget):
def __init__(self):
super().__init__()
self.x = 0
layout = QVBoxLayout()
self.label = QLabel("Another Window2")
layout.addWidget(self.label)
self.line = QLineEdit(self)
layout.addWidget(self.line)
self.setLayout(layout)
timer = QTimer(self)
timer.timeout.connect(self.update_line)
timer.start()
def update_line(self):
self.x += 3
self.line.setText(str(self.x))
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
l = QVBoxLayout()
button1 = QPushButton("Push for Window 1")
button1.clicked.connect(self.show_new_window)
l.addWidget(button1)
button2 = QPushButton("Push for Window 2")
button2.clicked.connect(self.show_new_window2)
l.addWidget(button2)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
def show_new_window(self):
self.w = AnotherWindow()
self.w.show()
def show_new_window2(self):
self.w2 = AnotherWindow2()
self.w2.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。