在 PyQt5 中显示和隐藏多个窗口 [英] Showing and hiding multiple windows in PyQt5

查看:234
本文介绍了在 PyQt5 中显示和隐藏多个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有 UI 的项目,并且我开始使用 PyQt5 来完成它.到目前为止,我看了很多视频,阅读了一些教程,并在项目上取得了进展.由于 PyQt 是 Python 的 C++ 绑定,因此对于具有大量窗口的复杂 UI 的文档并不多.(或者我找不到它,还查看了 PyQt5 的示例).我的项目包含很多窗口.我试图从用户那里获取一些参数,并且由于用户参数优化算法将起作用.所以项目包含这些窗口

I'm working on a project with UI and I started to do it with PyQt5. So far I watched lots of videos read some tutorials and have progress on the project. Since PyQt is a binding of C++ for Python, there are not so many documents for complex UI with lots of windows.(or I couldn't find it and also looked examples of PyQt5). My project contains lots of windows. I'm trying to take some arguments from a user and due to user arguments optimization algorithm will work. So the project contain's these windows

  1. 登录窗口(Qwidgets) {first view}
  2. 登录注册窗口(Qwidgets){如果用户没有帐户第二次查看}
  3. 如果用户登录到系统 Tabwidget 将显示 4 个子选项卡视图,这些选项卡将从用户那里获取参数.在这个小部件中,已经有 2 个子选项卡具有用户可以选择的值,而且还有一些按钮可以打开新的 Qwidget 类窗口,并带有 OK 和 CANCEL 按钮.用于选择文件目录的一个选项卡,我在这里使用了 QfileDialog 类.最后一个选项卡从用户那里获取最后一个参数并打开用户在第三个选项卡中选择的文件.
  4. 在 Tab 视图之后,我打开了一个 Plotview,其中包含用户选择的文件,并在该窗口中用户通过绘制一些多边形来给出绘图参数.{我在这里使用 Pyqtgraph 库}
  5. 优化算法将在不与 Pyqt 连接的情况下工作

我在设计 UI 时主要使用 Qt Designer.但后来改变了它添加了一些方法来从用户那里获取参数并连接其他选项卡或其他窗口.

I have used Qt Designer mostly while designing UI. But later changed it adding some methods to take arguments from a user and for connecting other tabs or other windows.

通过在类(窗口)中定义方法,我可以打开其他窗口,但不能隐藏或关闭已经打开的窗口.如果我尝试关闭它们,所有进程都会关闭,但我想让唯一的那个窗口关闭.我在登录注册窗口中遇到了这个问题,弹出窗口用于在第一个和第二个选项卡视图中从用户那里获取额外的参数.我的主要从登录窗口开始.之后在 Loginwindow 中,如果登录成功,它将进入 tabview 窗口.通过键入 mainwindow.hide() 并显示 tabview 窗口,我已经成功了.但在那之后,在所有弹出窗口中,我无法关闭接受用户参数的弹出窗口.

By defining methods in the class (window) I can open other windows but I cant hide or close an already opened window. if I try to close them all process goes down but I want to make the only that window close. I have that problem in Login Signup window, popup windows for taking extra arguments from a user in 1st and 2nd tabview. My main starts with Login Window. After that in Loginwindow if login success it goes to tabview window. I have been successful by typing mainwindow.hide() and showing tabview window. But after that in all popup windows, I cant close the popup windows that takes arguments from a user.

由于代码太长,我将把感兴趣的部分放在这里.

Since the code is so long I will just put here interested parts.

class LoginUp(object):

   def setupUi(self,LoginUp):
        self.Buton1.clicked.connect(self.signup)
        self.Buton2.clicked.connect(self.tabview)
   def signup(self):
        # here it shows but user cant close it by just clicking on OK button 
        # He must click on x button to close which I dont want.
        self.signupshow = QtWidgets.QWidget()                                  
        self.ui = LoginSignUp()
        self.ui.setupUi(self.signupshow)
        self.signupshow.show()

   def tabview(self):  # Here its works
        self.tabviewshow = QtWidgets.QWidget()
        self.ui_tabview = TabView()
        self.ui_tabview.setupUi(self.tabviewshow)
        MainWindow.close()
        self.tabviewshow.show()
class TabView(object):

    def setupUi(self,Form):
        self.button3.clicked.connect(self.addargument)

    def addargument(self):
        # same problem.. after that it popups window that user can give inputs but cant close the window AddArgument class 
        self.Add = QtWidgets.QWidget()
        self.addargumentshow = AddArgument()
        self.addargumentshow.setupUi(self.Add)
        self.addargumentshow.show()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QWidget()
    ui = LoginUp()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

推荐答案

解决此问题的一种方法是使用另一个类作为您要显示的所有窗口的控制器.

One way to solve this is to use another class as a controller for all of the windows you want to show.

在每个窗口中,当您想要切换窗口时,您将发送一个信号,并且由该控制器类决定在接收到信号时如何处理并决定显示哪个窗口.您需要传递的任何参数都可以通过信号传递.

In each window you will send a signal when you want to switch windows and it is up to this controller class to decide how to handle the signal when received and decide which window to show. Any arguments that you need passed can be passed through the signals.

这是一个简化的完整工作示例.

这篇关于在 PyQt5 中显示和隐藏多个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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