Bottom Page

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_())

Quote
#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.
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter window and turtle window error 1885 3 149 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Display Selected Image from Directory and Resize it EchoLi 0 142 Oct-02-2019, 06:54 PM
Last Post: EchoLi
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 352 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 3,024 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 515 May-12-2019, 08:17 PM
Last Post: joe_momma
  [WxPython] Adding a Window to a Button wxPython ShashankDS 4 537 Apr-23-2019, 06:53 PM
Last Post: Yoriz
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 675 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] not resize a frame Scorpio 2 417 Mar-13-2019, 11:24 PM
Last Post: Scorpio
  [Tkinter] Spawn sub-window with button press malonn 3 1,250 Oct-28-2018, 02:56 PM
Last Post: malonn
  tkinter button acts normal ONLY after clicking off the window justin_m 8 1,381 Apr-24-2018, 06:14 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)