J.Doe
J.Doe

Reputation: 434

Widgets are not displayed

I'm currently doing a tutorial on how to create GUI apps from a book called "Modern PyQt" by Joshua Willman and I'm stuck right out of the gate:

I've copy pasted a basic code snippet from the book and tried to tweak it bit by bit instead of reading a bunch of text without any practical trial and error. I can display a window and adjust it's size and properties, but when I try to attach other widgets to the main window, they just don't show up.

Here's what I've got so far:

# First practical example from the book: Pomodoro time manager
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon

class Pomodoro(QWidget):
    
    def __init__(self): # Create default constructor
        super().__init__()
        self.initializeUI()
    
    def initializeUI(self):
        """Initialize the window and display its contents to the screen."""
        self.setGeometry(int((SCREEN_WIDTH-WINDOW_WIDTH)/2),int((SCREEN_HEIGHT-WINDOW_HEIGHT)/2),WINDOW_WIDTH,WINDOW_HEIGHT) # x,y,w,h
        self.setWindowTitle('Bortism')
        # self.setWindowIcon(QIcon("Borticon.png"))
        self.button1 = QPushButton()
        self.button1.setText('Button')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    WINDOW_WIDTH, WINDOW_HEIGHT = 1000,600
    SCREEN_X, SCREEN_Y, SCREEN_WIDTH, SCREEN_HEIGHT = app.desktop().screenGeometry().getRect()
    window = Pomodoro()
    sys.exit(app.exec_())

Upvotes: 0

Views: 127

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

For a widget to be shown as part of another then the requirement is that the first is the child of the second. This can be done by passing the parent or by using a layout (which is also passed to it by the parent). So in your case you must change to:

self.button1 = QPushButton(self)

Upvotes: 2

Related Questions