简体   繁体   English

从另一个GUI文件PyQT5打开GUI文件

[英]Opening GUI file from another GUI file PyQT5

I am trying to make my GUI that runs on startup to open a different GUI when the user clicks a button. 我试图使我的GUI在启动时运行,以在用户单击按钮时打开其他GUI。 So on my startup file, I have: 因此,在我的启动文件中,我有:

class Startup(object):
    def setup_ui(self, Dialog):
        Dialog.setObjectName("Dialog")
...
        self.start_button = QtWidgets.QPushButton('', Dialog)
        self.start_button.clicked.connect(self.start_program)
...
    def start_program(self):
    # segmentation = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    # sys.exit(segmentation.exec_())
...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Startup()
ui.setup_ui(Dialog)
Dialog.show()
sys.exit(app.exec_( ))

My main file for the GUI that I am trying to start up looks like this: 我要启动的GUI的主文件如下所示:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        font = QtGui.QFont("Times", 30, QtGui.QFont.Bold)
        MainWindow.setObjectName("NBA Predictor")
        MainWindow.resize(1150, 790)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
...

However, when the user clicks the "start" button on the startup file, the main screen opens for like .1 seconds then closes immediately. 但是,当用户单击启动文件上的“开始”按钮时,主屏幕将打开0.1秒,然后立即关闭。 I think it is a problem with not having a correct exit command. 我认为没有正确的退出命令是一个问题。 However, if I uncomment the lines in the start_program function: 但是,如果我取消注释start_program函数中的行:

# segmentation = QtWidgets.QApplication(sys.argv)

and

# sys.exit(segmentation.exec_())

when I click on the start button, the window opens for a second then both windows close. 当我单击开始按钮时,窗口将打开一秒钟,然后两个窗口都关闭。 Any ideas? 有任何想法吗?

That happens because of Python GC. 发生这种情况是由于Python GC。 Save reference for MainWindow, like: 保存MainWindow的引用,例如:

self.mainWindow = QtWidgets.QMainWindow()
self.mainWindow.show()

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

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