【问题标题】:Resizing label text in PyQt5 Disigner在 PyQt5 Designer 中调整标签文本的大小
【发布时间】:2022-07-07 00:42:00
【问题描述】:

我目前正在 PyQt5 Designer 中制作 GUI 应用程序,但在获取标签文本和按钮以使用标签或按钮调整大小时遇到​​问题。我已经能够使用 PyQt5 Designer 的布局功能使按钮和标签随窗口调整大小,但无法调整文本大小。

谢谢! 凯

【问题讨论】:

  • Qt 不支持。您可能会以编程方式执行此操作,但根据整体大小设置字体大小只是您自己的选择。请注意,在不了解潜在问题的情况下执行此操作可能会导致问题(最有可能是递归,窗口会无限期地不断增加其大小)。此外,从桌面用户体验的角度来看,这可能是个坏主意。

标签: python pyqt5


【解决方案1】:

也许这是糟糕的用户体验设计,但我一直这样做。我的老眼睛希望一切都变大。我的技巧是将字体大小命令添加到主窗口的 resizeEvent 中。这是一个最小的例子

Python 代码:

from PyQt5.QtWidgets import QMainWindow, QLabel, QApplication, QLineEdit
from PyQt5.QtGui import (QFont, )
from PyQt5 import QtCore, QtWidgets, uic
import sys

form_class = uic.loadUiType('resize.ui')[0]

class Example(QMainWindow, form_class):

    def __init__(self, parent=None):
        super(Example, self).__init__(parent=parent)
        self.setupUi(self)
        self.show()


    def resizeEvent(self, event):
        # calculate scale factor height (sfh) and scale factor width (sfw) based on
        # minimumHeight and minimumWidth of centralWidget
        sfh = float(self.centralWidget().height())/float(self.centralWidget().minimumHeight())
        sfw = float(self.centralWidget().width())/float(self.centralWidget().minimumWidth())
        sf = min(sfh,sfw)  # scale factor. how much to increase/decrease font size

        font = QFont("Arial")
        font.setPointSizeF(int(sf*10.))

        for widget in self.findChildren((QLabel, QLineEdit)):
            # for more complicated MainWindow, add more widgets to tuple
            # argument
            widget.setFont(font)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

ui 文件(resize.ui):

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>160</width>
    <height>211</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <property name="minimumSize">
    <size>
     <width>160</width>
     <height>160</height>
    </size>
   </property>
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QLabel" name="label">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="text">
       <string>ResizeExample</string>
      </property>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QLineEdit" name="lineEdit">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="text">
       <string>this is a test</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>160</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

【讨论】:

    猜你喜欢
    • 2020-04-26
    • 2012-03-20
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    相关资源
    最近更新 更多