河南网络营销外包,保定百度推广优化排名,外国购物网站有哪些平台,九江室内设计学校文章目录 #x1f354;使用QtDesigner进行设计#x1f6f8;和子线程进行通信#x1f388;运行结果 #x1f354;使用QtDesigner进行设计
我们首先使用QtDesigner设计界面
得到代码login.ui
?xml version1.0 encodingUTF-8?
ui … 文章目录 使用QtDesigner进行设计和子线程进行通信运行结果 使用QtDesigner进行设计
我们首先使用QtDesigner设计界面
得到代码login.ui
?xml version1.0 encodingUTF-8?
ui version4.0classForm/classwidget classQWidget nameFormproperty namegeometryrectx0/xy0/ywidth478/widthheight220/height/rect/propertyproperty namewindowTitlestringForm/string/propertywidget classQLabel namelabelproperty namegeometryrectx20/xy30/ywidth72/widthheight15/height/rect/propertyproperty nametextstring用户名/string/property/widgetwidget classQLabel namelabel_2property namegeometryrectx21/xy74/ywidth71/widthheight21/height/rect/propertyproperty nametextstring密码/string/property/widgetwidget classQTextBrowser nametextBrowserproperty namegeometryrectx215/xy20/ywidth201/widthheight91/height/rect/property/widgetwidget classQPushButton namepushButtonproperty namegeometryrectx10/xy150/ywidth93/widthheight28/height/rect/propertyproperty nametextstring登录/string/property/widgetwidget classQPushButton namepushButton_2property namegeometryrectx140/xy150/ywidth93/widthheight28/height/rect/propertyproperty nametextstring忘记密码/string/property/widgetwidget classQLineEdit namelineEditproperty namegeometryrectx80/xy30/ywidth113/widthheight21/height/rect/property/widgetwidget classQLineEdit namelineEdit_2property namegeometryrectx80/xy70/ywidth113/widthheight21/height/rect/property/widget/widgetresources/connections/
/ui
和子线程进行通信
import json
import sys
import timefrom PyQt5 import uic
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QWidgetclass LoginThread(QThread):#创建自定义信号start_login_signalpyqtSignal(str)def __init__(self):super().__init__()def login_by_requests(self,user_password_json):# 将json字符串转换为自定义字符串实现传递用户名和密码# json.loads()方法能够将符合JSON格式的字符串转换为Python的字典或列表等数据类型。user_password_jsonjson.loads(user_password_json)print(user_password_json.get(user_name))print(user_password_json.get(password))def run(self):# 让子线程一直存活便于接收来自主线程的任务while True:print(子线程正在执行)time.sleep(1)
class MyWindow(QWidget):def __init__(self):super().__init__()self.init_ui()def init_ui(self):self.uiuic.loadUi(./login.ui)self.user_nameself.ui.lineEditself.passwordself.ui.lineEdit_2self.login_btnself.ui.pushButtonself.forget_btnself.ui.pushButton_2self.text_Browserself.ui.textBrowser #文本显示区域# 创建一个子线程self.login_threadLoginThread()# 绑定信号和槽函数self.login_btn.clicked.connect(self.login) #点击后调用login函数# 将要创建的子线程类中的信号进行绑定# 写下self.login_thread LoginThread()时实际上是调用了LoginThread类并创建了一个该类的实例对象并将其赋值给了self.login_thread变量。# 简而言之self.login_thread就是LoginThread()这个类的一个实例。# 当start_login_signal信号被触发时就会自动调用login_by_requests方法来进行登录操作。self.login_thread.start_login_signal.connect(self.login_thread.login_by_requests)# 让子线程开始工作self.login_thread.start()def login(self):# 登录按钮的槽函数user_nameself.user_name.text()passwordself.password.text()# 发送信号给子线程self.login_thread.start_login_signal.emit(json.dumps({user_name:user_name,password:password}))if __name____main__:appQApplication(sys.argv)wMyWindow()w.ui.show()app.exec_()运行结果