Setting QWidget size

Rich Shepard rshepard at appl-ecosys.com
Thu Apr 22 21:06:08 BST 2021


Looking in my two PyQt5 books and on the Web I'm not seeing how to
successfully set a QWidget main window's size. Test code attached.

The overall application will use a QMainWindow with a QTabWidget. Individual
pages have a QWidget as the container (unless my understanding is flawed).
I've tried to add width and height to the attached testgrid.py and it loads
without error, but as a very small window. My readings and web searches
haven't let me find the proper syntax to set a default initial window size
for each tab's contant to 800x600 (unless they need to be smaller to fit on
the QTabWidget in a QMainWindow.

I've also tried qtc.QSize(800,600) without success.

What's the proper syntax to open the test application at a size of 800x600?

TIA,

Rich
-------------- next part --------------
#!/usr/bin/env python3

import sys

from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc

class TestWindow(qtw.QWidget):
    def __init__(self):
        super().__init__()
        bio = qtw.QWidget(windowTitle='Test', width = 800, height = 600)
        
        # Layout
        container = qtw.QWidget(self)
        group1 = qtw.QGridLayout()
        container.setLayout(group1)

        savebutton = qtw.QPushButton('Save')
        savebutton.clicked.connect(self.save)
        group1.addWidget(savebutton, 0, 0)
        
        cancelbutton = qtw.QPushButton('Cancel')
        cancelbutton.clicked.connect(self.cancel)
        group1.addWidget(cancelbutton, 0, 1)

        self.show()

    # Methods
    def save(self):
        pass

    def cancel(self):
        pass
                

if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    bio = TestWindow()
    sys.exit(app.exec())       


More information about the PyQt mailing list