【问题标题】:Scaling QWidgets in PyQt在 PyQt 中缩放 QWidget
【发布时间】:2016-05-17 09:15:47
【问题描述】:

我在 Python 中构建了一个应用程序,使用 PyQt4 库作为我的 GUI。我的 GUI 在其上方绘制了一个图形和一些按钮。图表位于frame_B,图表上方的按钮(和LED)位于frame_A

我在MainWindow 类中创建frame_A(以及frame_B):

class CustomMainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(CustomMainWindow, self).__init__()
        # Do some initialization stuff..
    ''''''

    def initLayout(self):
        # Create frame_A and its corresponding layout
        frame_A = QtGui.QFrame(self)
        setCustomSize(frame_A, 380,800)
        self.layout_A = QtGui.QGridLayout()
        frame_A.setLayout(self.layout_A)

        # Create frame_B, and so forth..

    ''''''

    def setButtons(self):
        # Create btn01..
        # Create btn02..
        # Create btn03..
        # Create btn04..
        # Create btn05..
        # Create led01..
        # Add all buttons to the layout of frame_A:
        self.layout_A.addWidget(self.btn01, *(0,0))
        self.layout_A.addWidget(self.btn02, *(0,1))
        self.layout_A.addWidget(self.btn03, *(0,2))
        self.layout_A.addWidget(self.btn04, *(0,3))
        self.layout_A.addWidget(self.btn05, *(0,4))
        self.layout_A.addWidget(self.led01, *(0,5))

    ''''''

''''''

结果非常令人满意,如上图所示。当我放大我的窗口(通过简单的点击并用鼠标拖动)时,frame_Aframe_B 也会放大。那很好。我不想要任何其他方式。

但令我困扰的是frame_A 中的按钮被均匀分布以填充所有新空间。我更喜欢这样的行为:

任何人有一个聪明的想法来实现这一点?

【问题讨论】:

    标签: python qt python-3.x pyqt pyqt4


    【解决方案1】:

    在最后一个按钮后添加self.layout_A.addStretch()

    【讨论】:

    • 它不起作用。我收到错误:AttributeError: 'QGridLayout' object has no attribute 'addStretch()'
    【解决方案2】:

    如果您只想水平添加小部件,我建议使用QHBoxLayout 而不是QGridLayout。这样你就可以使用layout_A.setAlignment(QtCore.Qt.AlignLeft)。不确定这是否也适用于 Gridlayout。

    【讨论】:

    • 像魅力一样工作!谢谢先生:-)
    猜你喜欢
    • 2015-08-16
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多