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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
import sys
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog
import socket import threading
class Connect: def socket_init(self,ip):
self.s = socket.socket() self.s.connect((ip, 6666)) def read_from_server(s): global message_content while True: mes = s.recv(2048).decode('utf-8') message_content += mes print("message_content--->",message_content) form.set_label_content()
threading.Thread(target=read_from_server, args=(self.s, )).start()
def input_chat_content(self,line): if line is None or line == 'exit': return self.s.send(line.encode('utf-8'))
class Ui_Form(object): def __init__(self): self.connect_init()
def showDialog(self): self.name, ok = QInputDialog.getText(QWidget(), '用户登陆', 'Enter your name:') if ok: print(self.name)
print(self.name+"哈哈")
def setupUi(self, Form): Form.setObjectName("Form") Form.resize(528, 291) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(90, 40, 291, 121)) self.label.setObjectName("label") self.horizontalLayoutWidget = QtWidgets.QWidget(Form) self.horizontalLayoutWidget.setGeometry(QtCore.QRect(30, 200, 481, 80)) self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setObjectName("horizontalLayout") self.lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget) self.lineEdit.setObjectName("lineEdit") self.horizontalLayout.addWidget(self.lineEdit) self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget) self.pushButton.setObjectName("pushButton") self.pushButton.clicked.connect(self.send_message) self.horizontalLayout.addWidget(self.pushButton)
self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "")) self.pushButton.setText(_translate("Form", "发送"))
def connect_init(self): self.c = Connect() self.c.socket_init('127.0.0.1') self.c.input_chat_content("我来冒个泡!")
def send_message(self): name = "victor" con = self.lineEdit.text() t = str(time.strftime("%H:%M:%S", time.localtime())) new_con = "\n" + t + " " + name + "说:" + con
self.c.input_chat_content(new_con) """ # 设置本label显示 con = self.lineEdit.text() # 获取现在时间 t = str(time.strftime("%H:%M:%S", time.localtime())) new_con = self.label.text()+"\n"+t+" "+name+"说:"+con self.label.setText(new_con) # print() """ self.lineEdit.setText("")
print("--------------") print(message_content) print("--------------")
def set_label_content(self): self.label.setText(message_content)
if __name__ == '__main__':
message_content = "init_content"
app = QApplication(sys.argv)
w1 = QWidget()
form = Ui_Form() form.setupUi(w1) form.showDialog() form.set_label_content()
w1.show()
sys.exit(app.exec_())
|