Reputation: 2410
I have a PyQt Gui application. This application have a main window that should be open after the start.
This application should to listen the websocket.
I tried solve it so:
...
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:8080/chatsocket",
on_message = on_message,
on_error = on_error,
on_close = on_close)
# ws.on_open = on_open
ws.run_forever()
sys.exit(app.exec_())
But, after start application the main window was not open.
Without line "ws.run_forever()" the main window was open but application does not listen websocket.
I need listen the websocket in the "background"? Can you help me?
PS: (Sorry for my english)
Upvotes: 1
Views: 6077
Reputation: 2410
Thanks enginefree.
I make this
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__()
self.thread = ListenWebsocket()
self.thread.start()
...
class ListenWebsocket(QtCore.QThread):
def __init__(self, parent=None):
super(ListenWebsocket, self).__init__(parent)
websocket.enableTrace(True)
self.WS = websocket.WebSocketApp("ws://localhost:8080/chatsocket",
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close)
def run(self):
#ws.on_open = on_open
self.WS.run_forever()
def on_message(self, ws, message):
print message
def on_error(self, ws, error):
print error
def on_close(self, ws):
print "### closed ###"
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
QtGui.QApplication.setQuitOnLastWindowClosed(False)
window = Window()
window.show()
sys.exit(app.exec_())
Upvotes: 3