[PyQt] Resize button with window resize - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] Resize button with window resize (/thread-14477.html) |
Resize button with window resize - FesterJester - Dec-02-2018 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_()) RE: Resize button with window resize - Alfalfa - Dec-02-2018 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. RE: Resize button with window resize - FesterJester - Dec-03-2018 After a little while and a somewhat vague error, I figured out I needed to add from PyQt5 import QtWidgetsto 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. |