pyqt5如何打开新窗口并重新打开旧窗口 [英] pyqt5 how to open new window and reopen old

查看:975
本文介绍了pyqt5如何打开新窗口并重新打开旧窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好 我不知道该怎么办, login.py 窗口( register.py )打开得很好,但是窗口( login.py >)由于 register.py 而无法打开.

Hello I do not know what to do, the window (register.py) opens very well from login.py, but the window (login.py) does not not open since register.py.

该怎么办?

register.py

register.py

https://hastebin.com/oyoxoyemak.rb

login.py

login.py

https://hastebin.com/tanuhigome.rb

错误代码

如果我删除错误代码

        screen = app.primaryScreen()
        size = screen.size()
        print('Size: %d x %d' % (size.width(), size.height()))
        rect = screen.availableGeometry()
        print('Available: %d x %d' % (rect.width(), rect.height()))
        self.window.move((rect.width() / 2) - 230, (rect.height() / 2) - 230)

错误代码2

推荐答案

两个问题都与范围有关. app是两个脚本中的局部变量,仅存在于正在运行的脚本范围内(if __name__ == "__main__":行). 因此,如果您运行login.py,则app在其作用域中可用,但是如果您运行register.py,则app仅对其存在,而在login.py范围中不存在(因为它从未在那里"创建过) ).

Both issues are related to the scope. app is a local variable in both scripts, which exists only in the scope of the script that is running (the if __name__ == "__main__": line). So, if you run login.py, app is available in its scope, but if you run register.py app exists only for it, but not in the login.py scope (since it was never created "there").

由于primaryScreen是静态函数,因此您无需引用该应用程序实例(无论如何,您都可以通过QtWidgets.QApplication.instance()获得该引用):

Since primaryScreen is a static function, you don't need a reference to the application instance (which you could get through QtWidgets.QApplication.instance(), anyway):

只需将该行更改为:

    screen = QtWidgets.QApplication.primaryScreen()

在第二个问题中,问题是相似的:由于您正在运行login.py,因此永远不会在register.py中声明MainWindow_Register.

In the second issue, the problem is similar: since you're running login.py, MainWindow_Register is never declared in register.py.

我没有一个简单"的解决方案,因为您的方法有点困惑.
首先,似乎您正在尝试从pyuic的输出开始使程序无效,如果是这种情况,则应避免使用:编写自己的代码,并使用文档.
然后,每当您必须面对多个相关窗口时,最好避免递归"调用自己,而将单个窗口(或者更好的是,一个单独的对象,甚至是QApplication的子类)用作管理器".这样可以通过编程使一切变得更容易,避免了冗余代码,同时减少了发生错误的可能性.

I have not a "simple" solution for that, as your approach is a bit confused.
First of all, it seems like you're trying to implent your program starting from the output of pyuic, and if that's the case you should really avoid it: write your own code and use the pyuic generated files as suggested in the documentation.
Then, whenever you have to face multiple related windows, it's better to avoid calling themselves "recursively", and use a single window (or better, a separate object, even a subclass of QApplication) as a "manager". This will make everything easier programmatically, avoiding redundant code, while decreasing the possibility of bugs.

在您的情况下,您应该始终将登录窗口用作起点",然后在必要时显示一个注册器.

In your case, you should probably always use the login window as a "starting point", then show the register one whenever necessary.

这篇关于pyqt5如何打开新窗口并重新打开旧窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆