我不知道如何在问题中表达我的问题,因为我是一个非常PyQt5的学习者,我不知道词汇,但是来吧。
我需要返回session_user变量(在login()函数中)到我的主代码。这个变量需要在登录完成后传递到主屏幕,但我不知道PyQt5中有一个函数返回一个不是int类型的变量:accept (), reject (), done()。
是否有可能将我的session_user变量返回到我的主代码中,使用一些PyQt5函数,就像我提到的那样?
我代码:
from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import QDialog
from views.ui.login.telaLogin import Ui_Dialog
import sys
from control.exception_login import verification_login_user
class ViewLogin(QDialog):
def __init__(self):
super().__init__()
self.viewlogin = Ui_Dialog()
self.viewlogin.setupUi(self)
self.viewlogin.button_login.clicked.connect(self.login)
def login(self):
self.login = self.viewlogin.login_login.text()
self.password = self.viewlogin.login_password.text()
erro = verification_login_user(self.login, self.password)
if (erro == False):
self.close()
session_user = (self.login, self.senha)
#self.done(r) # I understand its functionality # PROBLEM
#return (session_user) # PROBLEM
self.accept() # The problem would end here if I didn't need the variable sessao_user
elif(erro == True):
self.viewlogin.login_login.setText('')
self.viewlogin.login_password.setText('')
app = QtWidgets.QApplication([])
w = ViewLogin()
result = w.exec_()
print(result) # desired result = (' login', 'password')
# real result = 1
sys.exit(app.exec_())
您可以通过threading
模块实现的方法之一。
- 创建类
Controller
和Daemon
到True
(这确保线程将在应用程序关闭后被杀死)。
class Controller(threading.Thread):
def __init__(self):
super(Controller, self).__init__(daemon = True)
self.my_variable = 0
self.variables_from_gui = None
- 覆盖
run
方法:
def run(self):
print("My main loop is here!")
while True:
print("Button presses: {}, Variables from gui: {}".format(self.button_presses, self.variables_from_gui))
time.sleep(1) #This wont freeze the gui
- 添加
button
登录点击注册功能:
def login(self, variables_from_gui = None):
#Do you login stuff
self.variables_from_gui = variables_from_gui
self.button_presses += 1
- 在App类中(在您的情况下,这将是主要的GUI类),从
login
函数启动一个新的thread
并将变量传递给它。
def login(self):
#Stuff to do in your GUI
variables_to_send = 0, "Text", {"key" : 10}
threading.Thread(target = lambda: self.controller.login(variables_from_gui = variables_to_send)).start()
- 同样在你的GUI类中创建一个
Controller
对象并启动它:
self.controller = Controller()
self.controller.start() #Start the run method in the Controller()
整个代码:有很多事情可以做得更好,使它更线程安全。但那不在这个问题的范围内。
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import threading
import time
class App(QWidget):
def __init__(self):
super().__init__()
self.left = 10
self.top = 10
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.controller = Controller()
self.controller.start() #Start the run method in the Controller()
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton("My login", self)
button.move(100,70)
button.clicked.connect(self.login)
self.show()
def login(self):
#Stuff to do in your GUI
variables_to_send = 0, "Text", {"key" : 10}
threading.Thread(target = lambda: self.controller.login(variables_from_gui = variables_to_send)).start()
class Controller(threading.Thread):
def __init__(self):
super(Controller, self).__init__(daemon = True)
self.button_presses = 0
self.variables_from_gui = None
def run(self):
print("My main loop is here!")
while True:
print("Button presses: {}, Variables from gui: {}".format(self.button_presses, self.variables_from_gui))
time.sleep(1) #This wont freeze the gui
def login(self, variables_from_gui = None):
#Do you login stuff
self.variables_from_gui = variables_from_gui
self.button_presses += 1
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())