Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

python - Showing and hiding multiple windows in 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. Login Window(Qwidgets) {first view}
  2. Login Sign up Window (Qwidgets) {if user don't have an account second view}
  3. If user logs into system Tabwidget will be shown with 4 subtab view and these tabs will take arguments from a user. In this widget, there is already 2 subtab have already values that user can choose but also there are buttons which open new Qwidget class windows with OK and CANCEL button. One tab for choosing a file directory and I used QfileDialog class here. And last tab taking last arguments from a user and opening the file that user has chosen in 3rd tab.
  4. After Tab view, I opened a Plotview, with the file that user have chosen, and in that window user by drawing some polygons giving argument on plot.{Im using Pyqtgraph library here}
  5. Optimization Algorithm will work it's not connected with Pyqt

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.

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_())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Here is a simplified complete working example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...