CodeLibrary/05_pycharm_python_pyqt5学习/main.py
JRNitre 1f49ce2ab5 追加 Python 项目
- Project 02
- Project 03
- Project 04
- Project 05
- Project 06
2024-06-15 00:04:43 +08:00

94 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import sys
import time
import requests
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import *
from PyQt5 import uic,QtCore
class LoginThread (QThread):
# 创建自定义信号
Start_Login_Signal = pyqtSignal(str)
def __init__(self, signal):
super().__init__()
self.Login_Complete_Signal = signal
def Login_by_Requests(self, User_Password_Json):
User_Data_Json = json.loads(User_Password_Json)
print(User_Data_Json.get("User_ID"))
print(User_Data_Json.get("User_Password"))
# 使用Requests模块发送请求 POST
r = requests.post(url="https://service-r80raze1-1300421481.sh.apigw.tencentcs.com/release/Python_Pyqt_Learn", json=User_Data_Json)
print("接受服务器响应:", r.content.decode())
ret = r.json()
print("发送信号给UI线程")
self.Login_Complete_Signal.emit(json.dumps(ret))
def run(self):
while True:
print ("子线程 running...")
time.sleep(1)
class UserMainWindow(QWidget):
Login_Status_Signal = pyqtSignal(str)
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.ui = uic.loadUi("./UserWindow.ui")
# 提取要操作的控件
self.Get_Widget()
# 绑定登录按钮至槽函数User_Login
self.BTN_Login.clicked.connect(self.User_Login)
# 子线程登录成功后向主线程发送信号
self.Login_Status_Signal.connect(self.Login_Status)
# 创建子线程
self.Login_Thread = LoginThread(self.Login_Status_Signal)
# 将要创建的子线程类中的信号进行绑定
self.Login_Thread.Start_Login_Signal.connect(self.Login_Thread.Login_by_Requests)
# 子线程开始工作
self.Login_Thread.start()
def Get_Widget(self):
# 用户信息输入框
# 用户ID
self.UserInputID_Edit = self.ui.UserID_Edit
# 用户密码
self.UserInputPwd_Edit = self.ui.UserPwd_Edit
# 信息输出框
self.MessageOutput_Text = self.ui.Message_Text
# 按钮:用户登录
self.BTN_Login = self.ui.BTN_Login
def User_Login(self):
# 获取用户输入文本
User_ID = self.UserInputID_Edit.text()
User_Password = self.UserInputPwd_Edit.text()
# 发送信号,子线程开始登录
self.Login_Thread.Start_Login_Signal.emit(json.dumps({"User_ID": User_ID, "User_Password": User_Password}))
def Login_Status(self, status):
Status_Dict = json.loads(status)
print(Status_Dict.get("errmsg"))
self.MessageOutput_Text.setText(Status_Dict.get("errmsg"))
self.MessageOutput_Text.repaint()
if __name__ == '__main__':
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
w = UserMainWindow()
# 展示窗口
w.ui.show()
app.exec()