程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python notes] qt+ cloud function realizes simple login box making

編輯:Python

【python note 】Qt+ Cloud functions Achieve a simple login box production

  • remarks : precondition :QtDesigner、pycharm、PyQt5、 Configured cloud functions ( Baidu's call function calculation CFC, Used to act as a crude server , Mainly free )

  • General train of thought :

    1. When you click the login box , hold username and password The data of the box is packaged into json Send to the... Provided by the cloud function url in ( Cloud functions html Trigger )

    2. Determine the user name and password in the cloud function , When the judgment is correct , Then return to 1, If it is judged as false, it returns 0

    3. Determine the return value locally , The return value is 1 It's in textbrowser Text appears in “ Login successful ”, Otherwise the text appears “ Login failed ”

  • Problems that need to be dealt with :

    1. When the login box and password box are clicked, multithreading is used , Otherwise, click later , The whole interface will get stuck , Do not operate the page until the event is completely over .

    Solution : Create a child thread when you click the login box , Run the slot function of the button in the sub thread

    1. Because it is a slot function for the child thread to run the login button event , Send packets to url, receive url The return value of , All in the sub thread , and textbrowser It's in the main thread , So it involves interprocess communication

    Solution : Use a custom signal to emit Return the return value to the main thread

1. use QtDesigner Draw a random login box

2. Configure cloud functions

This function is used to simulate server login

  1. Baidu intelligent cloud -> product -> Cloud native -> Function calculation
  2. Create a function , Select the blank function , Event mode selection event, Run time selection python3.6, Trigger selection HTTP Trigger ,HTTP Method select all or POST Will do , Directly create a function .
  3. Modify cloud functions
# use request Send a json Data package to the cloud function url,json The data will be stored in event In parameters , Here is the user name 、 The code is dead ,# In fact, you can connect to the database , Query in the database
import json
def handler(event, context):
username = json.loads(event.get("body")).get("username")
password = json.loads(event.get("body")).get("password")
if(username == "admin")&(password == "wdnmd"):
ret = 1
else:
ret = 0
return ret
  1. python Code
import sys
import time
import json
import requests
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import *
from PyQt5 import uic
class login_thread(QThread):
start_login_signal = pyqtSignal(str) # Used to handle url The returned value is submitted from the child thread to the main thread
def __init__(self,signal):
super().__init__()
self.signal1 = signal
def login_request(self,json_data):
num="0"
json_data1 = json.loads(json_data)
resp = requests.post(url="https://2729mj3a1p6k3.cfc-execute.bj.baidubce.com/loginfunc",json=json_data1)
resp.close()
num = resp.text
self.signal1.emit(num) # Activate custom model , The ginseng , The parameter is the login return value
def run(self): # Inherit QThread, rewrite run function , Write an infinite loop to prevent the thread from dying
while(1):
time.sleep(1)
class my_window(QWidget):
login_status=pyqtSignal(str)
def __init__(self):
super().__init__()
self.init()
def init(self):
self.ui = uic.loadUi("login.ui")# Load the written interface
# Put each ui The buttons in the file are bound
self.account_edit = self.ui.lineEdit
self.password_edit = self.ui.lineEdit_2
self.login_button = self.ui.pushButton
self.register_button = self.ui.pushButton_2
self.textb = self.ui.textBrowser
self.login_button.clicked.connect(self.login)
self.login_status.connect(self.status) # Bind slot function
self.thread = login_thread(self.login_status)
self.thread.start_login_signal.connect(self.thread.login_request) # Bind slot function
self.thread.start()
def login(self):
username = self.account_edit.text()
password = self.password_edit.text()
self.thread.start_login_signal.emit(json.dumps({"username":username,"password":password}))
def status(self,status): # Judge the login status by the return value , Set up textbrowser Words in
if status=='1':
self.textb.setText(" Login successful ")
self.textb.repaint() # Redraw the text every time you update it , Otherwise, it will not show
else:
self.textb.setText(" Login failed ")
self.textb.repaint()
app = QApplication(sys.argv)
w = my_window()
w.ui.show()
app.exec()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved