-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
# 我们一般将界面的显示用主线程来操作, | ||
# 逻辑功能代码或者耗时的代码都用另外线程进行处理 | ||
|
||
import sys | ||
import time | ||
|
||
from PyQt5.QtWidgets import * | ||
from PyQt5 import uic | ||
from PyQt5.Qt import QThread | ||
class MyWin(QWidget): | ||
def __init__(self): | ||
super().__init__() | ||
self.init_ui() | ||
|
||
def init_ui(self): | ||
self.ui = uic.loadUi('./thread-1.ui') | ||
|
||
lineedit = self.ui.lineEdit | ||
btn1 = self.ui.pushButton | ||
btn2 = self.ui.pushButton_2 | ||
|
||
btn1.clicked.connect(self.click_1) | ||
btn2.clicked.connect(self.click_2) | ||
|
||
def click_1(self): | ||
for i in range(10): | ||
print('是UI线程中执行......%d' % (i + 1)) | ||
time.sleep(1) | ||
|
||
def click_2(self): | ||
self.my_thread = MyThread() # 线程创建 | ||
self.my_thread.start() # 开始线程 | ||
|
||
|
||
class MyThread(QThread): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def run(self): | ||
for i in range(10): | ||
print('是MyThread中执行...... %d' % (i + 1)) | ||
time.sleep(1) | ||
|
||
if __name__ == "__main__": | ||
app = QApplication(sys.argv) | ||
myshow = MyWin() | ||
myshow.ui.show() | ||
app.exec() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
import sys | ||
from PyQt5.QtWidgets import QApplication, QWidget | ||
from PyQt5.Qt import QThread | ||
from PyQt5 import uic | ||
import time | ||
from PyQt5.QtCore import pyqtSignal | ||
import json | ||
|
||
class LoginThread(QThread): | ||
start_login_signal = pyqtSignal(str) | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def login_by_requests(self, user_password_json): | ||
user_password_json = json.loads(user_password_json) | ||
print(user_password_json.get('user_name')) | ||
print(user_password_json.get('password')) | ||
|
||
def run(self): | ||
# 通过while True的方式让子线程一直在运行,而不是结束 | ||
# 通过这种方式,我们让子线程一直活着,从而有能力接受来自主线程(UI线程)的任务 | ||
while True: | ||
print('子线程正在执行......') | ||
time.sleep(1) | ||
|
||
class MyWindow(QWidget): | ||
def __init__(self): | ||
super().__init__() | ||
self.init_ui() | ||
|
||
def init_ui(self): | ||
self.ui = uic.loadUi('./login.ui') | ||
|
||
self.user_name_Qwidget = self.ui.lineEdit | ||
self.password_Qwidget = self.ui.lineEdit_2 | ||
self.login_btn = self.ui.pushButton | ||
self.forget_password_btn = self.ui.pushButton_2 | ||
self.textBrowser = self.ui.textBrowser | ||
|
||
self.login_btn.clicked.connect(self.login) | ||
self.login_thread = LoginThread() | ||
# 将要创建的线程与 | ||
self.login_thread.start_login_signal.connect(self.login_thread.login_by_requests) | ||
# 让子线程开始工作 | ||
self.login_thread.start() | ||
|
||
def login(self): | ||
# 登录按钮的槽函数 | ||
user_name = self.user_name_Qwidget.text() | ||
password = self.password_Qwidget.text() | ||
self.login_thread.start_login_signal.emit(json.dumps({'user_name' : user_name, 'password' : password})) | ||
|
||
|
||
|
||
|
||
|
||
if __name__ == '__main__': | ||
app = QApplication(sys.argv) | ||
w = MyWindow() | ||
w.ui.show() | ||
app.exec() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>Form</class> | ||
<widget class="QWidget" name="Form"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>273</width> | ||
<height>102</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Form</string> | ||
</property> | ||
<widget class="QWidget" name=""> | ||
<property name="geometry"> | ||
<rect> | ||
<x>20</x> | ||
<y>20</y> | ||
<width>230</width> | ||
<height>65</height> | ||
</rect> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<item> | ||
<widget class="QLineEdit" name="lineEdit"/> | ||
</item> | ||
<item> | ||
<layout class="QHBoxLayout" name="horizontalLayout"> | ||
<item> | ||
<widget class="QPushButton" name="pushButton"> | ||
<property name="text"> | ||
<string>按钮1-卡</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QPushButton" name="pushButton_2"> | ||
<property name="text"> | ||
<string>按钮2-不卡</string> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</item> | ||
</layout> | ||
</widget> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |