lissethamc
lissethamc

Reputation: 79

opening a new window

I'm starting to learn OOP using PyQt5, so I'm trying to make a window with a button and when the button is clicked, I want to show a new window. I wrote this code but it doesn't work, it just shows the window with the button, but the button doesn't do anything, it seems like it just waits, but it doesn't give me any mistakes neither

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout

class Window2(QWidget):
    def __init__(self, parent=None):
        super(Window2, self).__init__(parent)


class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.init_ui()

    def init_ui(self):
        self.btn = QPushButton("Push Me")
        layout = QVBoxLayout()

        layout.addWidget(self.btn)

        self.setLayout(layout)
        self.setWindowTitle("PyQt5 double window")

        self.btn.clicked.connect(self.btn_clk)
        self.newindow = Window2(self)

        self.show()

    def btn_clk(self):
        self.newindow.show()

app = QApplication(sys.argv)
a_window = Window()
sys.exit(app.exec_())

This question guided me PyQT: how to open new window but it's written in PyQt4 and I'm not sure about the differences betweent these two.

Upvotes: 2

Views: 2271

Answers (1)

eyllanesc
eyllanesc

Reputation: 244262

In the answer of PyQT: how to open new window is using QMainWindow unlike you who is using QWidget.

What is the difference between QMainWindow and QWidget?

QMainWindow is a custom QWidget that has some flags activated, including the flag Qt::Window. As indicated by the docs indicates:

Indicates that the widget is a window, usually with a window system frame and a title bar, irrespective of whether the widget has a parent or not. Note that it is not possible to unset this flag if the widget does not have a parent.

That is, that widget will become a window even if it has a parent widget.

So the solution is to activate this flag in Window2:

import sys
from PyQt5 import QtCore, QtWidgets

class Window2(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Window2, self).__init__(parent, QtCore.Qt.Window) # <---

class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.init_ui()

    def init_ui(self):
        self.btn = QtWidgets.QPushButton("Push Me")
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.btn)
        self.setWindowTitle("PyQt5 double window")
        self.btn.clicked.connect(self.btn_clk)
        self.newindow = Window2(self)
        self.show()

    def btn_clk(self):
        self.newindow.show()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    a_window = Window()
    sys.exit(app.exec_())

Other alternative solutions are:

  • Make Window2 inherit from QMainWindow or QDialog.

  • Do not pass a parent to Window2: self.newindow = Window2()

Upvotes: 2

Related Questions