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
444 views
in Technique[技术] by (71.8m points)

python - How to communicate or switch between two windows in PyQt?

I am developing an application using python and Qt.

I have designed 2 Main windows ie..QMainWindow (Not QWidget or QDialog) using Qt.

Let it be.

1.LoginWindow -- LoginUI(Qt)

2.StuffWindow --- StuffUI

  1. First i should display Login Window.

  2. Then i should pass the username to StaffWindow (username needed for managing stuffs)

  3. StaffWindow should be shown and LoginWindow Should be Closed..

How can i achieve this..? Help me..

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Regardless of your description, I think your LoginWindow should be a QDialog, and your StuffWIndow be the MainWindow, and function like this...

  1. Your StuffWindow MainWindow should be created (not shown)
  2. Call a login() method that creates and exec_() your login QDialog as a application MODAL dialog
  3. Start the app.exec_() event loop now, and wait for the user to interact with login
  4. User interacts with login dialog, and the result of the dialog closing will then allow your app to check its values and choose to show its main interface.

Here is a quick outline:

class MainWindow():

    def login():
        loginDialog = LoginDialog()

        # this is modal. wait for it to close
        if loginDialog.exec_():
            # dialog was accepted. check its values and maybe:
            self.show()

        else:
            # maybe reshow the login dialog if they rejected it?
            loginDialog.exec_()


if __name__ == "__main__":

    app = QApp
    win = MainWindow()
    win.login()
    app.exec_()

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

...