在__init__
我爲了控制我的應用程序的狀態調用一個方法tickMain()
。最初States.STATE_INIT
傳入此方法。這意味着,state_Init()
然後調用之前,被傳遞到tickMain()
States.STATE_IDLE
。該計劃繼續致電tickMain()
。這個過程似乎工作正常(print()
聲明正在執行)。然而,用戶界面似乎停留在初始化階段,即它不顯示(只是一個白色背景,鼠標光標是加載圖標)。我錯過了什麼?我需要從另一個線程呼叫tickMain()
嗎?PyQt的UI不顯示在啓動時(我需要不同的線程?)
class CMyClass(QtGui.QMainWindow):
''''''''''''''''''''''''''''''''''''''''''
def __init__(self):
global state
while True:
self.tickMain()
time.sleep(1)
''''''''''''''''''''''''''''''''''''''''''
def tickMain(self):
global state
print("Ticking...")
switcher = { States.STATE_INIT: self.state_Init,
States.STATE_IDLE: self.state_Idle,
States.STATE_PROCESS_MSG_QUEUE: self.state_ProcessMsgQueue }
func = switcher.get(state, lambda: "nothing")
func()
''''''''''''''''''''''''''''''''''''''''''
def state_Init(self):
global state
print("Initializing...")
super(CMyClass, self).__init__()
print("Setting up COM port...")
self.com_serial = SerialCommsHandler.SerialCommsHandler()
print("Initializing UI...")
self.initUI()
print("Reading from COM port...")
state = States.STATE_IDLE
''''''''''''''''''''''''''''''''''''''''''
def initUI(self):
self.initMenuBar()
# Status Bar
sb_statusBar = self.statusBar()
# Send PushButton
self.btn_send = QtGui.QPushButton('Send', self)
self.btn_send.move(400, 245)
self.connect(self.btn_send, Qt.SIGNAL("clicked()"), self.slotMsgSend)
self.setGeometry(300, 300, 500, 400)
self.setFixedSize(650, 500)
self.setWindowTitle('My Test App')
self.show()
不要通過在'thickMain'方法內調用'self.tickMain()'來創建一個無限循環嗎? – PYPL
@PYPL我的確很愚蠢。我剛剛糾正了這一點。不過,我仍然有最初的問題。 – petehallw
這仍然是錯誤的! 'thickMain'應該返回一個值,這樣'__init__'中的'while'循環才能知道何時停止!你仍然在無限循環 – PYPL