Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyQt] Resize button with window resize
I have a basic Python3 PyQt5 GUI with a working quit button.
The window and frame resize fine and the button resizes horizontally, but not vertically.
After a few days of reading, searching, and failed attempts, I am here to ask for help.
Below is the code for my basic program.
Constructive criticism and suggestions welcome.

# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSlot

class App(QMainWindow):

    def __init__(self):
        self.resize(300, 200)
        self.setWindowTitle('Basic testing ground')
        self.table_widget = MyTableWidget(self)

    def centerscreen(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()

class MyTableWidget(QWidget):

    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout()

        # a quit button
        self.quitButton = QPushButton("Quit Now!")
        # connect to a pyqtSlot
        # Show button

    def quitClick(self):

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()

You have to set the vertical policy for your button (either as minimum or expanding):
        sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
When you want to change the layout but don't know where to start, a trick is to mess in QtDesigner until you find the right setting. Then save the ui file, convert it to python and see how it is done :)

As for the constructive critisism, you should avoid wildcard in imports as it is considered bad practice:
from PyQt5.QtWidgets import *
And always use explicit inheritances, such as:
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
It makes it clear on which module your application rely, and avoid name collision.
After a little while and a somewhat vague error, I figured out I needed to add
from PyQt5 import QtWidgets
to the imports section.

As for the wildcard, I only use it when testing small code.
I only import the modules I need on real projects.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Closing window on button click not working kenwatts275 4 175 May-03-2020, 01:59 PM
Last Post: deanhystad
  [PyQt] dynamically resize custom widget fill remaining space ironcthulhu 1 483 Dec-31-2019, 09:45 PM
Last Post: Denni
  8 image grid with automatical image resize by screen resolution AlexanderO 3 833 Dec-29-2019, 02:20 PM
Last Post: Xavier_Roga
  [PySimpleGUI] error trying to resize Text element skratt 3 748 Dec-10-2019, 06:05 PM
Last Post: FullOfHelp
  tkinter window and turtle window error 1885 3 940 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Display Selected Image from Directory and Resize it EchoLi 0 810 Oct-02-2019, 06:54 PM
Last Post: EchoLi
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 857 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 10,372 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 1,006 May-12-2019, 08:17 PM
Last Post: joe_momma
  [WxPython] Adding a Window to a Button wxPython ShashankDS 4 1,012 Apr-23-2019, 06:53 PM
Last Post: Yoriz

Forum Jump:

Users browsing this thread: 1 Guest(s)