调整PYQT5设计器中的标签文本大小

发布于 2025-02-13 12:03:19 字数 160 浏览 0 评论 0原文

我目前正在PYQT5 Designer中制作GUI应用程序,并且在标签和PushButtons的文字中遇到问题,以调整标签或PushButtons的大小。我已经能够使用Pyqt5 Designer的布局功能将PushButtons和标签与窗口进行调整大小,但只是无法调整文本大小。

谢谢! 肯

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. 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.

Thanks!
Ky

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2025-02-20 12:03:20

也许是不良的UX设计,但我一直这样做。我的旧眼睛希望一切都大。我的诀窍是将字体大小命令添加到主窗口的重新启动。这是一个最小示例

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>

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. Here is a minimal example

Python code:

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):

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文