pyqt5 how to open new window and reopen old

寵の児 提交于 2020-01-23 17:50:26

问题


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.

What to do?

register.py

https://hastebin.com/oyoxoyemak.rb

login.py

https://hastebin.com/tanuhigome.rb

error code

error code if i remove

        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)

error code 2


回答1:


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").

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):

Just change that line to:

    screen = QtWidgets.QApplication.primaryScreen()

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

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.



来源:https://stackoverflow.com/questions/58558321/pyqt5-how-to-open-new-window-and-reopen-old

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!