Python Forum
[PyQt] Resize button with window resize
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Resize button with window resize
#1
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):
        super().__init__()
        self.resize(300, 200)
        self.centerscreen()
        self.setWindowTitle('Basic testing ground')
        self.table_widget = MyTableWidget(self)
        self.setCentralWidget(self.table_widget)
        self.show()

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


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
        self.quitButton.clicked.connect(self.quitClick)
        # Show button
        self.layout.addWidget(self.quitButton)
        self.setLayout(self.layout)

    @pyqtSlot()
    def quitClick(self):
        app.quit()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
Reply
#2
You have to set the vertical policy for your button (either as minimum or expanding):
        sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
        self.quitButton.setSizePolicy(sizePolicy)
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 341 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,369 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,196 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 3,395 Aug-02-2021, 08:47 PM
Last Post: JayCee
  how to resize image in canvas tkinter samuelmv30 2 17,547 Feb-06-2021, 03:35 PM
Last Post: joe_momma
Question closing a "nested" window with a button in PySimpleGUI and repeating this process Robby_PY 9 13,408 Jan-18-2021, 10:21 PM
Last Post: Serafim
  Closing window on button click not working kenwatts275 4 3,663 May-03-2020, 01:59 PM
Last Post: deanhystad
  [PyQt] dynamically resize custom widget fill remaining space ironcthulhu 1 11,207 Dec-31-2019, 09:45 PM
Last Post: Denni
  8 image grid with automatical image resize by screen resolution AlexanderO 3 6,877 Dec-29-2019, 02:20 PM
Last Post: Xavier_Roga
  [PySimpleGUI] error trying to resize Text element skratt 3 7,994 Dec-10-2019, 06:05 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020