我正在尝试在我的python应用程序上运行一个PyQT GUI,并且我试图将它分成2个线程,因此GUI将在我的主要运行循环运行时响应,但我无法得到它去。也许我误解了它。这是我尝试过的:
我的Window
和Worker
主题定义如下:
class Window(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.thread = Worker()
start = QPushButton("Start", self)
QObject.connect(start, SIGNAL("clicked()"), MAIN_WORLD.begin)
hbox = QVBoxLayout(self)
hbox.addStretch(4)
class Worker(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
if __name__ == '__main__':
MAIN_WORLD = World()
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
似乎与在线示例非常接近。一旦用户点击“开始”,我的World
类就会运行一个无限循环,直到再次单击它为止。以下是其定义的一部分。
class World(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.currentlyRunning = False
//snip
def begin(self):
if self.currentlyRunning:
self.currentlyRunning = False
else:
self.currentlyRunning = True
self.MethodThatDoesUsefulStuff()
编辑:我注意到我并没有真正“使用”我的工作线程。如何将我的世界线程创建为工作线程?
答案 0 :(得分:1)
仔细查看代码后,您在QApplication之前启动了MAIN_WORLD,这不是您想要的。
你想做这样的事情:
if __name__ == '__main__':
app = QApplication(sys.argv)
sys.exit(app.exec_())
而且,在你的Window类中:
class Window(QWidget):
def __init__(self, *args):
self.world_thread = World();
# ...
以上将允许主线程控制gui并允许工作线程在后台工作。
答案 1 :(得分:0)
当然是PyQt而不是PySide,但是: