fork(2) download
  1. import time
  2. import socket
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtCore import *
  5. from PyQt5.QtGui import *
  6. from threading import Thread
  7.  
  8. class Chat(QWidget):
  9. def __init__(self):
  10. QWidget. __init__ (self)
  11. self.setWindowTitle('PyChat')
  12. self.resize(1200,800)
  13. self.setMaximumSize(QSize(1200, 800))
  14. self.setMinimumSize(QSize(600, 400))
  15. self.chatfield = QPlainTextEdit(self)
  16. self.chatfield.setGeometry(QRect(25, 25, 900, 705))
  17. self.connectme = QPushButton(self)
  18. self.connectme.setGeometry(QRect(950, 150, 140, 40))
  19. self.connectme.setText('Соединение')
  20. self.userName = QLineEdit(self)
  21. self.userName.setGeometry(QRect(950, 100, 175, 25))
  22. self.enterNamePls = QLabel(self)
  23. self.enterNamePls.setText('Введите ваше имя:')
  24. self.enterNamePls.setGeometry(QRect(950, 50, 100, 25))
  25. self.writehere = QLineEdit(self)
  26. self.writehere.setGeometry(QRect(25, 740, 900, 35))
  27. self.reply = QPushButton(self)
  28. self.reply.setGeometry(QRect(940, 740, 65, 35))
  29. self.reply.setText('Ответ')
  30. self.reply.clicked.connect(self.my_broadcast)
  31. self.listen_Th = Thread(target = self.my_listen)
  32. self.connectme.clicked.connect(lambda event='<Button-1>' : self.listen_Th.start())
  33. self.broadsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  34. self.broadsock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
  35. self.nick = 'Anonymous'
  36. self.show()
  37.  
  38. def my_listen(self):
  39. self.nick = self.userName.text()
  40. mysock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  41. mysock.bind(('',10032))
  42. self.connectme.setDisabled(True)
  43. while True:
  44. message = mysock.recv(256)
  45. self.chatfield.insertPlainText('['+time.strftime('%X')+ '] '+ message.decode()+'\n')
  46.  
  47. def my_broadcast(self):
  48. try:
  49. myreply = self.writehere.text()
  50. self.broadsock.sendto((self.nick+': ' + myreply).encode(), ('255.255.255.255', 10032))
  51. self.writehere.clear()
  52. except:
  53. pass
  54.  
  55. if __name__ == '__main__':
  56. import sys
  57. myapp = QApplication(sys.argv)
  58. mywin = Chat()
  59. sys.exit(myapp.exec_())
Runtime error #stdin #stdout #stderr 0.03s 9064KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 3, in <module>
ImportError: No module named 'PyQt5'