forked from slogmore/BasicChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_gui.py
62 lines (47 loc) · 1.65 KB
/
client_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore
import PyQt4.uic as uic
import qt4reactor
from client_protocol import ClientProtocolFactory
class ClientWindow(QtGui.QMainWindow):
def __init__(self, reactor):
self.reactor = reactor
self.initializeUI()
self.initializeReactor(reactor)
def initializeUI(self):
QtGui.QMainWindow.__init__(self)
self.ui = uic.loadUi('GUI\clientui.ui', self)
self.ui.show()
self.ui.lineEditCmdLine.returnPressed.connect(self.handleCmdLine)
self.ui.lineEditCmdLine.setFocus()
def initializeReactor(self, reactor):
self.reactor = reactor
self.factory = ClientProtocolFactory(self.handleReceivedMessage)
self.connection = self.reactor.connectTCP("127.0.0.1", 10000, self.factory)
def handleReceivedMessage(self, cmd, msg):
if(cmd == "CHAT"):
self.addToChat(msg)
def handleSendMessage(self, msg):
self.factory.sendMessage(msg)
def handleCmdLine(self):
msg = str(self.ui.lineEditCmdLine.text())
self.handleSendMessage(msg)
self.ui.lineEditCmdLine.clear()
def addToChat(self, msg):
self.ui.txtMain.append(str(msg))
def connectFail(self, reason):
self.addToChat("Disconnected")
self.connection.diconnect()
def closeEvent(self, event):
self.connection.disconnect()
self.reactor.stop()
event.accept()
def main():
app = QtGui.QApplication(sys.argv)
qt4reactor.install()
from twisted.internet import reactor
clientWindow = ClientWindow(reactor)
reactor.run()
if __name__ == '__main__':
main()