【问题标题】:Python PyQt Qlabel ResizePython PyQt Qlabel 调整大小
【发布时间】:2016-12-23 06:21:54
【问题描述】:

我在这里尝试将我的 Qlabel 尺寸设置得更大时遇到了麻烦。这是我的代码。我不知道该怎么办。我已经尝试了很多...

def __init__(self, parent=None):
    super(UICreator, self).__init__(parent)
    self.Creator = QPushButton("YouTube", self)
    self.Creator.resize(100, 40)
    self.Creator.move(25, 50)
    self.CreatorB2 = QPushButton("Twitter", self)
    self.CreatorB2.resize(100, 40)
    self.CreatorB2.move(275, 50)
    self.CreatorL = QLabel("Created By:", self)
    self.CreatorL.resize(100, 100)
    self.CreatorL.move(20, 300)

【问题讨论】:

  • 只是一个问题:您确定要为所有小部件固定几何形状吗?除非您有严格的显示尺寸,否则编写可扩展的 UI 是最好的方法。使用布局将负责调整大小(尽管您可以设置每个小部件的大小策略以确保以特定方式处理每个小部件的大小调整事件)。
  • “更大”到底是什么意思?具体来说,您尝试过哪些不起作用的方法?

标签: python pyqt qlabel


【解决方案1】:

如果您使用的是 PyQt4,请确保您已导入:

from PyQt4 import QtCore

然后添加这一行来设置标签的大小:

self.CreatorL = QLabel("Created By:", self)
self.CreatorL.setGeometry(QtCore.QRect(70, 80, 100, 100)) #(x, y, width, height)

【讨论】:

  • 对不起,我没有说,但我正在使用 pyqt4
  • 只需将 PyQt5 替换为 PyQt4。
  • @Achilles。您的答案与 OP 已经发布的内容有何不同?为什么你认为它会产生不同的结果?
  • @ekhumoro 你确定吗?请说明。
  • @Achilles。你知道moveresizesetGeometry是做什么的吗?
【解决方案2】:

setGeometry 工作完美,除非您使用的布局需要特定的尺寸,为此我使用setFixedSize 这应该有助于确保您的小部件不会因网格而意外压缩或扩展布局或类似的东西。

所以应该是这样的:

from PyQt5 import QtWidgets

my_label = QtWidgets.QLabel()
my_label.setText('My Label')
my_label.setFixedSize(50, 10)

【讨论】:

    【解决方案3】:

    添加到 Achilles 评论,因为我发现这很有用...

    实际值(x、y、宽度、高度)

    如果你想做一个相对的改变,那么这样的事情会起作用:

        self.CreatorL = QLabel("Created By:", self)
        self.CreatorL.setGeometry(QtCore.QRect(self.CreatorL.x()+50, self.CreatorL.y(), self.CreatorL.width(), self.CreatorL.height())) 
    

    此示例将标签向右移动 50 像素。 Self.CreatorL 可以替换为标签对象的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2011-05-16
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多