PyQt中QPushButton列表绑定槽触发死锁问题求助
PyQt动态生成按钮触发死锁问题
我是PyQt新手,尝试开发一个包含可变数量按钮的应用窗口,这些按钮用于切换对应独立窗口的显示状态。为实现可变数量的按钮,我创建了QPushButton列表并循环生成按钮,但遇到异常问题:运行以下代码时,仅点击第4个按钮(对应window 3)会触发死锁报错;进一步测试发现,当按钮数量超过10时,特定索引(如第4个、第12个、最后一个)的按钮都会触发该死锁问题,且在不同机器上测试均存在此现象。
报错的动态生成按钮代码
import sys from random import randint from PyQt5 import QtWidgets class AnotherWindow(QtWidgets.QWidget): """ This "window" is a QWidget. If it has no parent, it will appear as a free-floating window. """ def __init__(self): super().__init__() layout = QtWidgets.QVBoxLayout() self.label = QtWidgets.QLabel("Another Window % d" % randint(0, 100)) layout.addWidget(self.label) self.setLayout(layout) class MainWindow(QtWidgets.QMainWindow): def __init__(self,windows): super().__init__() self.windows=[] self.buttons=[] l=QtWidgets.QVBoxLayout() for i in range(len(windows)): window=AnotherWindow() self.windows.append(window) button=QtWidgets.QPushButton(f'window {windows[i]}') print(i," ",button) self.buttons.append(button) self.buttons[i].clicked.connect(self.toggle_window,i) l.addWidget(self.buttons[i]) w = QtWidgets.QWidget() w.setLayout(l) self.setCentralWidget(w) print(len(self.windows)) def toggle_window(self,i): if self.windows[i].isVisible(): self.windows[i].hide() else: self.windows[i].show() if __name__=="__main__": app = QtWidgets.QApplication(sys.argv) windows=[0,1,2,3] windows=[str(i) for i in windows] print(windows) w = MainWindow(windows) w.show() app.exec()
报错信息
Qt: Dead lock detected while activating a BlockingQueuedConnection: Sender is QPushButton( ... ), receiver is PyQtSlotProxy( ... )
验证用静态声明按钮代码(无异常)
import sys from random import randint from PyQt5 import QtWidgets class AnotherWindow(QtWidgets.QWidget): """ This "window" is a QWidget. If it has no parent, it will appear as a free-floating window. """ def __init__(self): super().__init__() layout = QtWidgets.QVBoxLayout() self.label = QtWidgets.QLabel("Another Window % d" % randint(0, 100)) layout.addWidget(self.label) self.setLayout(layout) class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.window0 = AnotherWindow() self.window1 = AnotherWindow() self.window2 = AnotherWindow() self.window3 = AnotherWindow() l = QtWidgets.QVBoxLayout() button0 = QtWidgets.QPushButton("window 0") button0.clicked.connect(self.toggle_window0) l.addWidget(button0) button1 = QtWidgets.QPushButton("window 1") button1.clicked.connect(self.toggle_window1) l.addWidget(button1) button2 = QtWidgets.QPushButton("window 2") button2.clicked.connect(self.toggle_window2) l.addWidget(button2) button3 = QtWidgets.QPushButton("window 3") button3.clicked.connect(self.toggle_window3) l.addWidget(button3) w = QtWidgets.QWidget() w.setLayout(l) self.setCentralWidget(w) def toggle_window0(self, checked): if self.window0.isVisible(): self.window0.hide() else: self.window0.show() def toggle_window1(self): if self.window1.isVisible(): self.window1.hide() else: self.window1.show() def toggle_window2(self): if self.window2.isVisible(): self.window2.hide() else: self.window2.show() def toggle_window3(self, checked): if self.window3.isVisible(): self.window3.hide() else: self.window3.show() if __name__=="__main__": app = QtWidgets.QApplication(sys.argv) w = MainWindow() w.show() app.exec()
请问我哪里操作有误?该问题的原因是什么?
环境:Ubuntu 22.04,PyQt版本:1.9(conda环境)
内容的提问来源于stack exchange,提问作者cestpasmoi
相关产品推荐
相关产品推荐

