PyQt5 multi-window

Question:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'PyosUI.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (
    QApplication,
    QLabel,
    QMainWindow,
    QPushButton,
    QVBoxLayout,
    QWidget
)
class GameWindow(QWidget):
    def __init__(self):
        super.__init__()
        layout = QVBoxLayout()
        self.label = QLabel("Hello!")
        layout.addWidget(self.label)
        self.setLayout(layout)
        
class Ui_Dialog(object):      
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.setEnabled(True)
        Dialog.resize(1044, 601)
        self.Notepad = QtWidgets.QPushButton(Dialog)
        self.Notepad.setGeometry(QtCore.QRect(50, 520, 281, 61))
        self.Notepad.setObjectName("Notepad")
        self.Game = QtWidgets.QPushButton(Dialog)
        self.Game.setGeometry(QtCore.QRect(390, 520, 271, 61))
        self.Game.setObjectName("Game")
        self.Poweroff = QtWidgets.QPushButton(Dialog)
        self.Poweroff.setGeometry(QtCore.QRect(690, 520, 111, 31))
        self.Poweroff.setObjectName("Poweroff")
        self.Settings = QtWidgets.QPushButton(Dialog)
        self.Settings.setGeometry(QtCore.QRect(690, 560, 111, 31))
        self.Settings.setObjectName("Settings")
        self.Date_time = QtWidgets.QDateTimeEdit(Dialog)
        self.Date_time.setGeometry(QtCore.QRect(840, 20, 194, 22))
        self.Date_time.setObjectName("Date_time")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(0, -20, 1051, 641))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("background.png"))
        self.label.setObjectName("label")
        self.label.raise_()
        self.Notepad.raise_()
        self.Game.raise_()
        self.Poweroff.raise_()
        self.Settings.raise_()
        self.Date_time.raise_()

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        l = QVBoxLayout()
        self.Game.clicked.connect(self.button_clicked)
        l.addWidget(self.Game)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "PyOS"))
        self.Notepad.setText(_translate("Dialog", "Notepad"))
        self.Game.setText(_translate("Dialog", "Game"))
        self.Poweroff.setText(_translate("Dialog", "Poweroff"))
        self.Settings.setText(_translate("Dialog", "Settings"))
    def game_window(self):
        game_window = QtWidgets.QDialog()
        game_window.setWindowTitle("Game")
        game_window.resize(600, 600)
        game_window.show()

    def button_clicked(self):
        self.game_window()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

The above code is the UI I made with Pyqt5 designer. For the work I want to make, when the ‘Game’ button is clicked, a window dedicated to the ‘Game’ button should appear.

However, when I run the code above, when I press the ‘Game’ button, a 600 x 600 window appears and then disappears.

Why?
How else can I solve this problem?

Thank you.

Asked By: Dot0707

||

Answers:

The game_window is a local variable within the game_window() method in Ui_Dialog.
Set the window as an instance and it should be fine.

Modified method

# other code remains same.

    def game_window(self):
        self._game_window = QtWidgets.QDialog()
        self._game_window.setWindowTitle("Game")
        self._game_window.resize(600, 600)
        self._game_window.show()

Edit: The reason to add the underscore (_) is to avoid having the same name as the method name.

Answered By: Emmanuel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.