Python正在等待条件满足

2024-09-29 21:46:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个GUI,向用户询问用户/密码。创建GUI的类调用另一个创建web浏览器的类,并尝试使用同一类的方法登录网站。如果登录成功,GUI对象的变量将变为“True”

我的主文件是下一个:

from AskUserPassword import AskGUI
from MainInterfaceGUI import MainGUI

Ask = AskGUI()
Ask = Ask.show()

MainInterface = MainGUI()

if Ask.LoginSuccesful == True:
    Ask.close()
    MainInterface.show()
    

如果登录成功,我想隐藏用户/密码GUI并显示主GUI。上面的代码显然不起作用

如何使Python等待这种类型的条件得到满足


Tags: 方法用户fromimportwebtrue密码网站
1条回答
网友
1楼 · 发布于 2024-09-29 21:46:27

与其不断地检查要满足的条件,为什么不在登录时提供您想要做的作为对AskGUI对象的回调,然后让AskGUI对象在尝试登录时调用回调呢?比如:

def on_login(ask_gui):
    if ask_gui.LoginSuccesful:
        ask_gui.close()
        MainInterface.show()


Ask = AskGUI()
Ask.login_callback = on_login

然后,在AskGUI中,当单击登录按钮并检查凭据时,您将执行以下操作:

def on_login_click(self):
    ... 
    # Check login credentials.
    self.LoginSuccessful = True

    # Try to get self.login_callback, return None if it doesn't exist.
    callback_function = getattr(self, 'login_callback', None) 
    if callback_function is not None:
        callback_function(self)

Re

I prefer to have all the structure in the main file. This is a reduced example but If I start to trigger from a method inside a class that is also inside another class... it's going to be hard to understand

我推荐这种方式,因为所有处理登录时发生的事情的代码都包含在需要进行登录的类中。处理要显示的UI元素(on_login())的代码包含在处理该元素的类中。 您不需要在后台不断检查Ask.LoginSuccessful是否已更改。 当您使用一个像样的IDE时,很容易跟踪每个函数的定义位置

相关问题 更多 >

    热门问题