Skip to main content
 首页 » 编程设计

python之QGridLayout 小部件重叠

2024年12月31日9bonelee

我正在尝试创建一个程序,让用户可以通过三个选项来回答问题。我为此使用了 QGridLayout,但只显示了最后添加到 QGridLayout 的小部件。

预期:

################################ 
#             Text             # 
################################ 
 
########## ########## ########## 
#Button 1# #Button 2# #Button 3# 
########## ########## ########## 

现实:

########## 
#Button 3# 
########## 

简化的 Python 3 代码(无文本小部件):

from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
import sys 
 
QApp = QApplication(sys.argv) 
 
QMWRoot = QMainWindow() 
QGLRoot = QGridLayout(QMWRoot) 
QMWRoot.setLayout(QGLRoot) 
QPB0 = QPushButton('B0x0', QMWRoot) 
QGLRoot.addWidget(QPB0, 0, 0) 
QPB1 = QPushButton('B0x1', QMWRoot) 
QGLRoot.addWidget(QPB1, 0, 1) 
QPB2 = QPushButton('B1x0', QMWRoot) 
QGLRoot.addWidget(QPB2, 1, 0) 
QMWRoot.show() 
 
sys.exit(QApp.exec_()) 

原始(未完成)Python 3 代码:

from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
import sys 
 
class Root(QMainWindow): 
    def __init__(self, QApp): 
        super().__init__() 
        self.QApp = QApp 
    def setupUi(self): 
        self.setWindowTitle('') 
        self.QGLRoot = QGridLayout() 
        self.setLayout(self.QGLRoot) 
 
        self.QLBTool = QLabel() 
        self.QLBTool.setText('Tool') 
        self.QGLRoot.addWidget(self.QLBTool, 0, 0) 
        self.QPB0 = QPushButton(self) 
        self.QPB0.setText('0') 
        self.QGLRoot.addWidget(self.QPB0, 1, 0) 
        self.QPB1 = QPushButton(self) 
        self.QPB1.setText('1') 
        self.QGLRoot.addWidget(self.QPB1, 1, 1) 
        self.QPB2 = QPushButton(self) 
        self.QPB2.setText('2') 
        self.QGLRoot.addWidget(self.QPB2, 1, 2) 
    def startUi(self): 
        self.show() 
    def updateUi(self): 
        pass 
 
QApp = QApplication(sys.argv) 
 
App = Root(QApp) 
App.setupUi() 
App.startUi() 
 
sys.exit(QApp.exec_()) 

请您参考如下方法:

QMainWindow 你有一个特殊的结构:

因此在您的情况下,您必须建立一个中央小部件并在那里放置布局。

import sys 
 
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
 
 
class Root(QMainWindow): 
    def __init__(self, QApp): 
        super().__init__() 
        self.QApp = QApp 
 
    def setupUi(self): 
        self.setWindowTitle('') 
        widget = QWidget() 
        self.setCentralWidget(widget) 
 
        self.QGLRoot = QGridLayout() 
        widget.setLayout(self.QGLRoot) 
 
        self.QLBTool = QLabel() 
        self.QLBTool.setAlignment(Qt.AlignHCenter) 
        self.QLBTool.setText('Tool') 
 
        self.QGLRoot.addWidget(self.QLBTool, 0, 0, 1, 3) 
 
        self.QPB0 = QPushButton() 
        self.QPB0.setText('0') 
        self.QGLRoot.addWidget(self.QPB0, 1, 0) 
        self.QPB1 = QPushButton() 
        self.QPB1.setText('1') 
        self.QGLRoot.addWidget(self.QPB1, 1, 1) 
        self.QPB2 = QPushButton() 
        self.QPB2.setText('2') 
        self.QGLRoot.addWidget(self.QPB2, 1, 2) 
 
    def startUi(self): 
        self.show() 
 
QApp = QApplication(sys.argv) 
App = Root(QApp) 
App.setupUi() 
App.startUi() 
sys.exit(QApp.exec_()) 

紧凑和通用的代码:

import sys 
 
from PyQt5.QtCore import Qt 
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, \ 
    QGridLayout, QLabel, QPushButton 
 
 
class Root(QMainWindow): 
    def __init__(self, QApp): 
        super().__init__() 
        self.setupUi() 
 
    def setupUi(self): 
        self.setWindowTitle('') 
 
        widget = QWidget() 
        self.setCentralWidget(widget) 
        QGLRoot = QGridLayout(widget) 
 
        options = ("0", "1", "2") 
 
        QLBTool = QLabel('Tool') 
        QLBTool.setAlignment(Qt.AlignHCenter) 
 
        QGLRoot.addWidget(QLBTool, 0, 0, 1, len(options)) 
 
        for i, option in enumerate(options): 
            button = QPushButton(option) 
            QGLRoot.addWidget(button, 1, i) 
 
QApp = QApplication(sys.argv) 
App = Root(QApp) 
App.show() 
sys.exit(QApp.exec_())