【问题标题】:PyQt GUI control access from another threadPyQt GUI 控制来自另一个线程的访问
【发布时间】:2013-06-06 00:10:20
【问题描述】:

我正在尝试在 python 中创建客户端服务器应用程序。当服务器关闭时,我希望客户端的 gui 在单独的线程上关闭,但是应用程序因 Xlib 错误而崩溃:错误的实现...我已经搜索过,似乎是从其他线程访问 GUI 界面。我该怎么办?python gui从其他线程访问

【问题讨论】:

    标签: python multithreading user-interface


    【解决方案1】:

    这可能对你有帮助..

    from PyQt4 import QtGui as gui
    from PyQt4 import QtCore as core
    
    import sys
    import time
    
    
    class ServerThread(core.QThread):
        def __init__(self, parent=None):
            core.QThread.__init__(self)
    
        def start_server(self):
            for i in range(1,6):
                time.sleep(1)
                self.emit(core.SIGNAL("dosomething(QString)"), str(i))
    
        def run(self):
            self.start_server()
    
    
    class MainApp(gui.QWidget):
        def __init__(self, parent=None):
            super(MainApp,self).__init__(parent)
    
            self.label = gui.QLabel("hello world!!")
    
            layout = gui.QHBoxLayout(self)
            layout.addWidget(self.label)
    
            self.thread = ServerThread()
            self.thread.start()
    
            self.connect(self.thread, core.SIGNAL("dosomething(QString)"), self.doing)
    
        def doing(self, i):
            self.label.setText(i)
            if i == "5":
                self.destroy(self, destroyWindow =True, destroySubWindows = True)
                sys.exit()
    
    
    app = gui.QApplication(sys.argv)
    form = MainApp()
    form.show()
    app.exec_()
    

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多