调整标签大小时,Pyqt标签会更改窗口大小

时间:2019-09-14 17:39:31

标签: python pyqt5

我正在学习pyqt,并尝试在小部件的中间制作一段文本,当根据小部件窗口的大小更改小部件大小时,该文本会更改字体大小。除了可以在某个点水平扩展窗口时,它也会在垂直方向自动扩展窗口这一事实,我还有自己想要的东西。

我怀疑这与填充有关。水平拖动窗口大小时,所需的行为是使文本缩放大小,直到将导致窗口的垂直尺寸发生变化为止,此时不再更改字体大小。

以下是我到目前为止用一堆打印语句计算出的尺寸,但是由于某种原因,没有任何一个与窗口的尺寸匹配。

from PyQt5.QtWidgets import (QWidget,QHBoxLayout,QLabel,QApplication,QSizePolicy)
from PyQt5.QtGui import QFont, QFontMetrics
from PyQt5.QtCore import Qt,pyqtSignal
import sys

class alarm(QWidget):
    resized = pyqtSignal()
    def __init__(self):
        super().__init__()

        self.setGeometry(300, 300, 150, 150)
        self.setWindowTitle("Alarm Clock")

        self.init_UI()

    def init_UI(self):
        self.counter = 0
        self.resized.connect(self.set_font_size)
        self.time = QLabel('Test')
        self.time.setFont(QFont('Times New Roman'))
        self.time.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)

        self.time.setAlignment(Qt.AlignCenter)
        hbox = QHBoxLayout()
        hbox.addWidget(self.time)

        self.setLayout(hbox)
    def resizeEvent(self,event):
        self.resized.emit()

    def set_font_size(self):
        curr_geo = self.geometry().width()
        font = self.time.font()
        curr_pixel_size = font.pixelSize()
        new_pixel_size = curr_geo/3

        font.setPixelSize(new_pixel_size)
        t = self.time.contentsRect().height()
        print('Label contents rect: ',t)
        print('label height: ' + str(t))
        new_bounding = QFontMetrics(font).boundingRect(self.time.text())

        print('New bounding: ' + str(new_bounding.height()))
        print('Total height: ' + str(self.geometry().height()))
        if new_bounding.height() > self.geometry().height():
            font.setPixelSize(curr_pixel_size)
            self.time.setFont(font)
        else:
            font.setPixelSize(new_pixel_size)
            self.time.setFont(font)

if __name__ == "__main__":
    app = QApplication([])
    alarm = alarm()
    alarm.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

使用标签time的高度

if new_bounding.height() > self.time.height():

甚至没有else:

    if new_bounding.height() <= self.time.height():
        font.setPixelSize(new_pixel_size)
        self.time.setFont(font)

完整代码:

from PyQt5.QtWidgets import (QWidget,QHBoxLayout,QLabel,QApplication,QSizePolicy)
from PyQt5.QtGui import QFont, QFontMetrics
from PyQt5.QtCore import Qt,pyqtSignal
import sys

class alarm(QWidget):
    resized = pyqtSignal()
    def __init__(self):
        super().__init__()

        self.setGeometry(300, 300, 150, 150)
        self.setWindowTitle("Alarm Clock")

        self.init_UI()

    def init_UI(self):
        self.counter = 0
        self.resized.connect(self.set_font_size)
        self.time = QLabel('Test')
        self.time.setFont(QFont('Times New Roman'))
        self.time.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)

        self.time.setAlignment(Qt.AlignCenter)
        hbox = QHBoxLayout()
        hbox.addWidget(self.time)

        self.setLayout(hbox)
    def resizeEvent(self,event):
        self.resized.emit()

    def set_font_size(self):
        curr_geo = self.geometry().width()
        font = self.time.font()
        curr_pixel_size = font.pixelSize()
        new_pixel_size = curr_geo/3

        font.setPixelSize(new_pixel_size)
        t = self.time.contentsRect().height()
        print('Label contents rect: ',t)
        print('label height: ' + str(t))
        new_bounding = QFontMetrics(font).boundingRect(self.time.text())

        print('New bounding: ' + str(new_bounding.height()))
        print('Geometry height: ' + str(self.geometry().height()))
        print('Label height: ' + str(self.time.height()))
        if new_bounding.height() <= self.time.height():
            font.setPixelSize(new_pixel_size)
            self.time.setFont(font)

if __name__ == "__main__":
    app = QApplication([])
    alarm = alarm()
    alarm.show()
    sys.exit(app.exec_())