When it comes to graphical interface design in Python, it is often necessary to set the minimum size of a control to ensure that it can be displayed normally

When it comes to graphical interface design in Python, it is often necessary to set the minimum size of a control to ensure that it can be displayed normally. In Qt, we can use two different methods to set the minimum size of the control, namely minimumSizeHint and minimumSize.

First, let's look at the minimumSizeHint method. This method returns a suggested minimum size value, calculated based on the content of the control. If you want a control to take as little space as possible while still being able to display its content, you can use the minimumSizeHint method to set the minimum size of the control.

Next, let's take a look at how to use the minimumSizeHint method to set the minimum size of a QPushButton control. Note that we set the button's text to "Click Me" so we can see that the minimumSizeHint takes the size of the text into account when calculating the size:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        btn = QPushButton('Click Me', self)
        btn.resize(btn.minimumSizeHint())
        btn.move(50, 50)       

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('minimumSizeHint')
        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex

Guess you like

Origin blog.csdn.net/update7/article/details/131496759