简体   繁体   English

windows PyQt设计器之间切换

[英]Switching between windows PyQt designer

I have been trying to develop a software in PyQt5.我一直在尝试在 PyQt5 中开发软件。

I have 3 windows: 1) login 2) dashboard 3) appointments我有 3 个 windows:1)登录 2)仪表板 3)约会

Now I can navigate properly from login -> dashboard -> appointments perfectly.现在我可以完美地从登录 -> 仪表板 -> 约会中正确导航。 But when I press pushbutton for back from appointments, it doesn't come back to dashboard.但是,当我按下约会返回按钮时,它不会返回到仪表板。

But if I go from dashboard->appointments, the pushbutton works for 1 time.但是,如果我从仪表板->约会中 go,则按钮可以工作 1 次。

I have removed irrelevant lines from the code to make analysis simpler.我已经从代码中删除了不相关的行,以使分析更简单。

login window class:登录 window class:

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow

    def retranslateUi(self, MainWindow):
        self.pushButton.clicked.connect(self.next)
 
    def next(self):
        ui = Ui_MainWindow2()
        ui.setupUi(self.MainWindow)
        self.MainWindow.show()

dashboard window class:仪表板 window class:

class Ui_MainWindow2(object):

    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow

    def retranslateUi(self, MainWindow):
        self.pushButton.clicked.connect(self.next)
 
    def next(self):
        ui = Ui_MainWindow3()
        ui.setupUi(self.MainWindow)
        self.MainWindow.show()

appointment window class:预约 window class:

class Ui_MainWindow3(object):

    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow

    def retranslateUi(self, MainWindow):
        self.pushButton.clicked.connect(self.back)
 
    def back(self):
        ui = Ui_MainWindow2()
        ui.setupUi(self.MainWindow)
        self.MainWindow.show()

Application starts from file login.py which has:应用程序从文件 login.py 开始,该文件具有:

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

The forward navigation always work, the backward one keeps failing...向前导航总是有效,向后导航总是失败......

Any help or suggestions will be very welcome...任何帮助或建议都将非常受欢迎......

Turns out I was writing the next or back function slightly differently than it should have been.原来我写的下一个或后面的 function 与应该写的略有不同。 The next and back functions should have been like this: next 和 back 函数应该是这样的:

def next(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow2()
        self.ui.setupUi(self.window)
        self.oldWindow.hide()
        self.window.show()

where self.oldWindow is the reference to the original window of the class enclosing the function next.其中 self.oldWindow 是对包含 function 的 class 的原始 window 的引用。 This is also same for the back function.背面 function 也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM