关闭当前窗口,并在pyqt的条件下打开新窗口

发布于 2025-02-05 19:24:10 字数 516 浏览 2 评论 0原文

如何关闭当前窗口并打开新窗口(如果条件为true)(没有任何按钮),然后再次运行孔脚本。我尝试了很多方法,但没有成功

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    .....
 
if __name__ == "__main__":
    # print(deivce_data)
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
    if deivce_data == data:
        '''if this condition is true close the current window and run script again'''

How to close the current window and open new window if condition is true(without any button clicked) and run the hole script again. I have tried so many method but not success

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    .....
 
if __name__ == "__main__":
    # print(deivce_data)
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
    if deivce_data == data:
        '''if this condition is true close the current window and run script again'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亣腦蒛氧 2025-02-12 19:24:10

您可以通过控制qapplication()的执行句柄来做到这一点。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton


class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()


if __name__ == "__main__":
    # print(deivce_data)
    app1 = QApplication(sys.argv)
    ui1 = Ui_MainWindow()
    ui1.setWindowTitle("My App1")
    ui1.show()
    finish_app1 = app1.exec_()

    if True:
        '''if this condition is true close the current window and run script again'''
        app2 = QApplication(sys.argv)
        ui2 = Ui_MainWindow()
        ui2.setWindowTitle("My App2")
        ui2.show()
        finish_app2 = app2.exec_()
        handle_finish = finish_app2
    else:
        handle_finish = finish_app1

    sys.exit(handle_finish)

希望这会有所帮助。

You can do this by controlling the execution handle of QApplication().

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton


class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()


if __name__ == "__main__":
    # print(deivce_data)
    app1 = QApplication(sys.argv)
    ui1 = Ui_MainWindow()
    ui1.setWindowTitle("My App1")
    ui1.show()
    finish_app1 = app1.exec_()

    if True:
        '''if this condition is true close the current window and run script again'''
        app2 = QApplication(sys.argv)
        ui2 = Ui_MainWindow()
        ui2.setWindowTitle("My App2")
        ui2.show()
        finish_app2 = app2.exec_()
        handle_finish = finish_app2
    else:
        handle_finish = finish_app1

    sys.exit(handle_finish)

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文