2013-06-05 112 views
0

我想在python中創建客戶端服務器應用程序。當服務器關閉時,我希望客戶端的GUI可以在單獨的線程上關閉,但是應用程序會用Xlib錯誤來壓縮:不好的實現...我已經搜索並且似乎是從其他線程訪問GUI界面。我應該怎麼辦?從其他線程從另一個線程PyQt GUI控制訪問

回答

1

蟒蛇GUI訪問,這可能幫助你..

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_() 
+0

我使用PyQt的3.3版本。我不知道這是否會工作.. – user2451774

+0

即時通訊使用pyqt 4.10,不知道這是否會在3.3 – abhishekgarg