布局的中心控件位置问题
创始人
2024-12-26 00:00:48
0

在 PyQt5 中,可以使用 QGridLayout 布局来实现控件的自动排列。对于中心控件位置的问题,可以通过将控件添加到 QGridLayout 布局中心位置的方法来解决。

示例代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        gridLayout = QGridLayout()
        label1 = QLabel('Label 1', self)
        label2 = QLabel('Label 2', self)

        gridLayout.addWidget(label1, 0, 0)
        gridLayout.addWidget(label2, 1, 1)

        # 设置第 3 行、第 3 列为中心位置
        gridLayout.setRowStretch(2, 1)
        gridLayout.setColumnStretch(2, 1)

        self.setLayout(gridLayout)
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Central widget position of layout')
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MyWidget()
    sys.exit(app.exec_())

此例中,将 label1 和 label2 控件添加到了 (0,0) 和 (1,1) 的位置。同时,将第 3 行和第 3 列的比例设为 1,即使控件占据中心位置。运行程序后,可以看到 label2 控件已经占据了中心位置。

相关内容

热门资讯

透视中牌率!wepoker私人... 透视中牌率!wepoker私人局开挂视频,wepoker俱乐部辅助(透视)总结教程(有挂辅助)1、下...
透视透视!wepoker可以开... 透视透视!wepoker可以开透视吗,wepoker能不能透视(透视)2025新版总结(有挂揭秘)所...
透视了解!wepoker好友助... 透视了解!wepoker好友助力码,wepoker有插件吗(透视)新2025教程(有挂解说);1、w...
透视app!wepokerpl... 透视app!wepokerplus开挂,wepoker私人局规律(透视)2025新版技巧(有挂工具)...
透视了解!wepokerh5破... 透视了解!wepokerh5破解,wepoker私人局辅助器怎么用(透视)分享教程(有挂介绍)1、w...
透视ai!wepoker开辅助... 透视ai!wepoker开辅助能查到吗,wepoker好友局透视(透视)教你攻略(有挂攻略)1、这是...
透视智能ai!we-poker... 透视智能ai!we-poker软件,wepoker免费透视脚本(透视)安装教程(有挂解密)1、不需要...
透视科技!wepoker有没有... 透视科技!wepoker有没有插件,wepoker辅助器下载(透视)透明挂教程(有挂辅助);1、打开...
透视游戏!wepokerplu... 透视游戏!wepokerplus透视脚本免费,wepoker作弊辅助(透视)爆料教程(有挂教程)小薇...
透视新版!wepoker透视器... 透视新版!wepoker透视器免费,wepoker模拟器哪个好用(透视)存在挂教程(有挂解密)1、任...