简体   繁体   English

在 PyQt5 Disigner 中调整标签文本的大小

[英]Resizing label text in PyQt5 Disigner

I'm currently making a GUI application in PyQt5 Designer and I'm having problems getting the text of labels and pushbuttons to resize with the labels or pushbuttons.我目前正在 PyQt5 Designer 中制作 GUI 应用程序,但在获取标签文本和按钮以使用标签或按钮调整大小时遇到​​问题。 I've been able to get the pushbuttons and labels to resize with the window using PyQt5 Designer's layout feature, but just can't get the text to resize.我已经能够使用 PyQt5 Designer 的布局功能使按钮和标签随窗口调整大小,但无法调整文本大小。

Thanks!谢谢! Ky

Maybe it's bad UX design, but I do this all the time.也许这是糟糕的用户体验设计,但我一直这样做。 My old eyes want everything to be big.我的老眼睛希望一切都变大。 My trick is to add font size commands to the resizeEvent of the main window.我的技巧是将字体大小命令添加到主窗口的 resizeEvent 中。 Here is a minimal example这是一个最小的例子

Python code: 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 file (resize.ui): 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM