Advertisement
unknown009

chattbox

Oct 6th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. from PyQt4.QtGui import *
  2. from PyQt4.QtCore import *
  3. from PyQt4 import QtGui
  4. from PyQt4 import QtCore
  5. from socket import *
  6. import thread,sys,select,string
  7.  
  8. HOST = '192.168.10.34'
  9. PORT = 5001
  10. BUFSIZE = 1024
  11. ADDR = (HOST, PORT)
  12. global data
  13.  
  14. tcpCliSock = socket(AF_INET, SOCK_STREAM)
  15. tcpCliSock.connect(ADDR)
  16.  
  17. class MainWindow(QtGui.QMainWindow):
  18. def __init__(self, parent=None):
  19. super(MainWindow, self).__init__(parent)
  20. self.thread = listener()
  21. self.thread.recv.connect(self.add)
  22. self.thread.start()
  23.  
  24.  
  25. roomLabel = QtGui.QLabel('room')
  26.  
  27. self.browser = QtGui.QTextBrowser()
  28. self.browser.backwardAvailable
  29.  
  30. self.textEdit = QtGui.QTextEdit()
  31. self.textEdit.setMaximumSize(QtCore.QSize(400,60))
  32. self.connect(self.browser, QtCore.SIGNAL("returnPressed()"),self.callback)
  33.  
  34. SendButton = QtGui.QPushButton('Send')
  35. SendButton.setMaximumSize(QtCore.QSize(400,60))
  36. SendButton.clicked.connect(self.callback)
  37.  
  38.  
  39.  
  40.  
  41. layoutINlayout = QtGui.QHBoxLayout()
  42. layoutINlayout.addWidget(self.textEdit)
  43. layoutINlayout.addWidget(SendButton)
  44.  
  45.  
  46. widget = QtGui.QWidget()
  47. self.setCentralWidget(widget)
  48.  
  49. self.layout = QtGui.QVBoxLayout()
  50. self.layout.addWidget(self.browser)
  51.  
  52. mainwindow = QtGui.QVBoxLayout()
  53. mainwindow.addLayout (self.layout )
  54. mainwindow.addLayout (layoutINlayout )
  55.  
  56. widget.setLayout(mainwindow)
  57. self.setWindowFlags(QtCore.Qt.WindowTitleHint )
  58.  
  59. def callback(self, event):
  60.  
  61. message = self.textEdit.toPlainText()
  62. tcpCliSock.send(message)
  63.  
  64. def add(self):
  65. data = tcpCliSock.recv(4096)
  66. self.browser.setText(data)
  67.  
  68.  
  69. class listener(QThread,QtCore.QObject):
  70. recv = QtCore.pyqtSignal()
  71. def __init__(self):
  72. QThread.__init__(self)
  73. self.exiting = False
  74.  
  75. def __del__(self):
  76. self.exiting = True
  77. self.wait()
  78.  
  79.  
  80.  
  81.  
  82. def run(self):
  83.  
  84.  
  85. print 'Connected to remote host. Start sending messages'
  86.  
  87. while 1:
  88. data = tcpCliSock.recv(BUFSIZE)
  89. if data:
  90. self.recv.emit()
  91. if __name__ == '__main__':
  92.  
  93. app = QtGui.QApplication(sys.argv)
  94. app.setStyle('chat')
  95.  
  96.  
  97. window = MainWindow()
  98. window.setWindowTitle("CHAT BOX")
  99. window.show()
  100. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement