gpt4 book ai didi

python - QGridLayout 小部件重叠

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:36 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,让用户可以通过三个选项来回答问题。我为此使用了 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 你有一个特殊的结构:

enter image description here

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

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_())

enter image description here

紧凑和通用的代码:

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_())

关于python - QGridLayout 小部件重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51131805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com