我正在尝试创建一个程序,其中用户具有三个选项来回答问题。我为此使用了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_())
答案 0 :(得分:1)
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_())