0

I'm new to python and pyqt. I'm trying to open a new window after the first screen. My second window opens but without the options I specified, label and pushbutton.

from PyQt5 import QtWidgets
import sys

class secondwindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(secondwindow, self).__init__()
        self.label1 = QtWidgets.QLabel("Second Window");
        self.button1 = QtWidgets.QPushButton("Click Me");
        hbox = QtWidgets.QHBoxLayout()
        hbox.addWidget(self.label1)
        hbox.addWidget(self.button1)
        self.setLayout(hbox)

class Window(QtWidgets.QWidget):
    def btnclicked(self):
        sender = self.sender()
        if sender.text() == "OK":
            self.secwin.show()

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.button1 = QtWidgets.QPushButton("OK");
        vbox = QtWidgets.QVBoxLayout()
        vbox.addWidget(self.button1)
        self.setLayout(vbox)

        self.button1.clicked.connect(self.btnclicked)
        self.secwin = secondwindow()
        self.show()

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = Window()
    main.show
    sys.exit(app.exec())

if __name__ == '__main__':
    main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Reese_E
  • 1
  • 2

1 Answers1

0

QMainWindow is a special widget because it has a defined structure, http://doc.qt.io/qt-5/qmainwindow.html#qt-main-window-framework:

enter image description here

As shown in the image there is already an area destined to place the widgets called Central Widget, in it you must place the widgets that you want to be displayed for it, you use setCentralWidget().

In your case the solution is:

class secondwindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(secondwindow, self).__init__()
        central_widget = QtWidgets.QWidget()
        self.label1 = QtWidgets.QLabel("Second Window")
        self.button1 = QtWidgets.QPushButton("Click Me")
        hbox = QtWidgets.QHBoxLayout(central_widget)
        hbox.addWidget(self.label1)
        hbox.addWidget(self.button1)
        self.setCentralWidget(central_widget)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241