0

This code from a tutorial works fine in PyQt5 if I call it from __main__

    window = QWidget()
    layout = QVBoxLayout()
    layout.addWidget(QPushButton('Top'))
    layout.addWidget(QPushButton('Bottom'))
    window.setLayout(layout)
    window.show()

If I put the code class in the __init__ of a class:

class MainWindow (QMainWindow):

    def __init__ (self):

        print ("HERE 1")
        window = QWidget()
        layout = QVBoxLayout()
        layout.addWidget(QPushButton('Top'))
        layout.addWidget(QPushButton('Bottom'))
        window.setLayout(layout)
        window.show()
        print ("HERE 2")
        return

then in __main__:

app = QApplication (args)

mw = MainWindow.MainWindow ()

return app.exec_ ()

Then the print statemets happen but no widgets display.

Why would window not display in the second case? (The corresponding C++ code would display it.)

spraff
  • 32,570
  • 22
  • 121
  • 229
  • MainWindow inherits from QMainWindow so you must call super, that's a basic OOP concept. – eyllanesc Jul 08 '20 at 13:59
  • Another error is that `window` is a local variable that will be destroyed so the window will not show, the solution is to extend the scope, perhaps making it a member of the class, change: `window` to `self.window` – eyllanesc Jul 08 '20 at 14:02
  • In your last comment you talk about a C++ code that you never show, probably in that code logic is implemented that points out in the comments but in another way, such as using pointers that its scope is extensive. Don't extract properties from C++ to Python – eyllanesc Jul 08 '20 at 14:05
  • I was expecting the QWidget do display and continue existing independently, so I think it's the variable scope that's the actual explanation, if you care to write it as an answer. – spraff Jul 11 '20 at 11:43

1 Answers1

0

You need a bit more code to make this run. You are trying to show the widget window but you are not showing the QmainWindow. If the widget is on the QMainWindow it will be visible when you show the main window. You also need to call init from the QMainWindow class to set everything up. Finally, with a QMainWindow, you need to set a central widget.

I generally take a different approach in main. Take a look at my code below:

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

class MainWindow (QMainWindow):

    def __init__ (self):
        QMainWindow.__init__(self, parent=None)

        print ("HERE 1")
        window = QWidget()
        layout = QVBoxLayout()
        layout.addWidget(QPushButton('Top'))
        layout.addWidget(QPushButton('Bottom'))
        window.setLayout(layout)
        self.setCentralWidget(window)
        #window.show()
        print ("HERE 2")
        return

if __name__=="__main__":    
    app = QApplication(sys.argv)
    mw = MainWindow ()
    mw.show()
    sys.exit(app.exec_())

If you want to show only the widget not on a QmainWindow, try this:

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

class MainWindow (QWidget):

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

        print ("HERE 1")
        layout = QVBoxLayout()
        layout.addWidget(QPushButton('Top'))
        layout.addWidget(QPushButton('Bottom'))
        self.setLayout(layout)
        print ("HERE 2")

if __name__=="__main__":    
    app = QApplication(sys.argv)
    mw = MainWindow ()
    mw.show()
    sys.exit(app.exec_())
Dkellygb
  • 866
  • 2
  • 8
  • 24
  • Why do I need to do this in Python but not C++? Isn't PyQt just bindings for the C++? The QMainWindow is not a parent of the QWidget, they're both top-level windows. – spraff Jul 08 '20 at 10:18
  • Your class Mainwindow inherits from QmainWindow so I assumed you wanted to have a mainwindow. (class MainWindow (QMainWindow):) QMainWindow has menus and other stuff you would probably want as the main screen of a program. Of course you can inherit from QWidget then you would not have to setCentralWidget. Change your window.setLayout to self.setLayout. – Dkellygb Jul 08 '20 at 14:55