【PyQt5篇】和子线程进行通信

骑猪看日落 2024-04-17 06:45 99阅读 0赞

文章目录

  • ?使用QtDesigner进行设计
  • ?和子线程进行通信
    • ?运行结果

在这里插入图片描述

?使用QtDesigner进行设计

我们首先使用QtDesigner设计界面
在这里插入图片描述
在这里插入图片描述

得到代码login.ui

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>Form</class>
  4. <widget class="QWidget" name="Form">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>478</width>
  10. <height>220</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>Form</string>
  15. </property>
  16. <widget class="QLabel" name="label">
  17. <property name="geometry">
  18. <rect>
  19. <x>20</x>
  20. <y>30</y>
  21. <width>72</width>
  22. <height>15</height>
  23. </rect>
  24. </property>
  25. <property name="text">
  26. <string>用户名:</string>
  27. </property>
  28. </widget>
  29. <widget class="QLabel" name="label_2">
  30. <property name="geometry">
  31. <rect>
  32. <x>21</x>
  33. <y>74</y>
  34. <width>71</width>
  35. <height>21</height>
  36. </rect>
  37. </property>
  38. <property name="text">
  39. <string>密码:</string>
  40. </property>
  41. </widget>
  42. <widget class="QTextBrowser" name="textBrowser">
  43. <property name="geometry">
  44. <rect>
  45. <x>215</x>
  46. <y>20</y>
  47. <width>201</width>
  48. <height>91</height>
  49. </rect>
  50. </property>
  51. </widget>
  52. <widget class="QPushButton" name="pushButton">
  53. <property name="geometry">
  54. <rect>
  55. <x>10</x>
  56. <y>150</y>
  57. <width>93</width>
  58. <height>28</height>
  59. </rect>
  60. </property>
  61. <property name="text">
  62. <string>登录</string>
  63. </property>
  64. </widget>
  65. <widget class="QPushButton" name="pushButton_2">
  66. <property name="geometry">
  67. <rect>
  68. <x>140</x>
  69. <y>150</y>
  70. <width>93</width>
  71. <height>28</height>
  72. </rect>
  73. </property>
  74. <property name="text">
  75. <string>忘记密码</string>
  76. </property>
  77. </widget>
  78. <widget class="QLineEdit" name="lineEdit">
  79. <property name="geometry">
  80. <rect>
  81. <x>80</x>
  82. <y>30</y>
  83. <width>113</width>
  84. <height>21</height>
  85. </rect>
  86. </property>
  87. </widget>
  88. <widget class="QLineEdit" name="lineEdit_2">
  89. <property name="geometry">
  90. <rect>
  91. <x>80</x>
  92. <y>70</y>
  93. <width>113</width>
  94. <height>21</height>
  95. </rect>
  96. </property>
  97. </widget>
  98. </widget>
  99. <resources/>
  100. <connections/>
  101. </ui>

?和子线程进行通信

  1. import json
  2. import sys
  3. import time
  4. from PyQt5 import uic
  5. from PyQt5.QtCore import QThread, pyqtSignal
  6. from PyQt5.QtWidgets import QApplication, QWidget
  7. class LoginThread(QThread):
  8. #创建自定义信号
  9. start_login_signal=pyqtSignal(str)
  10. def __init__(self):
  11. super().__init__()
  12. def login_by_requests(self,user_password_json):
  13. # 将json字符串转换为自定义字符串,实现传递用户名和密码
  14. # json.loads()方法能够将符合JSON格式的字符串转换为Python的字典或列表等数据类型。
  15. user_password_json=json.loads(user_password_json)
  16. print(user_password_json.get("user_name"))
  17. print(user_password_json.get("password"))
  18. def run(self):
  19. # 让子线程一直存活,便于接收来自主线程的任务
  20. while True:
  21. print("子线程正在执行")
  22. time.sleep(1)
  23. class MyWindow(QWidget):
  24. def __init__(self):
  25. super().__init__()
  26. self.init_ui()
  27. def init_ui(self):
  28. self.ui=uic.loadUi("./login.ui")
  29. self.user_name=self.ui.lineEdit
  30. self.password=self.ui.lineEdit_2
  31. self.login_btn=self.ui.pushButton
  32. self.forget_btn=self.ui.pushButton_2
  33. self.text_Browser=self.ui.textBrowser #文本显示区域
  34. # 创建一个子线程
  35. self.login_thread=LoginThread()
  36. # 绑定信号和槽函数
  37. self.login_btn.clicked.connect(self.login) #点击后,调用login函数
  38. # 将要创建的子线程类中的信号进行绑定
  39. # 写下self.login_thread = LoginThread()时,实际上是调用了LoginThread类,并创建了一个该类的实例对象,并将其赋值给了self.login_thread变量。
  40. # 简而言之,self.login_thread就是LoginThread()这个类的一个实例。
  41. # 当start_login_signal信号被触发时,就会自动调用login_by_requests方法来进行登录操作。
  42. self.login_thread.start_login_signal.connect(self.login_thread.login_by_requests)
  43. # 让子线程开始工作
  44. self.login_thread.start()
  45. def login(self):
  46. # 登录按钮的槽函数
  47. user_name=self.user_name.text()
  48. password=self.password.text()
  49. # 发送信号给子线程
  50. self.login_thread.start_login_signal.emit(json.dumps({
  51. "user_name":user_name,"password":password}))
  52. if __name__=='__main__':
  53. app=QApplication(sys.argv)
  54. w=MyWindow()
  55. w.ui.show()
  56. app.exec_()

?运行结果

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PyQt5】多线

    这是因为QThread类提供了一个事件循环,可以在后台处理线程的任务,而不会影响到线程的响应性。,当在主线程中执行耗时操作时(例如click_1方法中的for循环),它。...