我有一个简单的窗口,我想在 a 中水平显示一个按钮和一个标签,QHboxLayout
然后在 a 中垂直显示QGroupBox
一些。我试图把这两件放在一个但没有运气。我的代码的结果QRadioButton
QVBoxLayout
QVBoxLayout
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Application(QtGui.QWidget):
""" Main application window """
def __init__(self):
super(Application, self).__init__()
self.initUI()
def initUI(self):
self.openBtn = QtGui.QPushButton("Open", self)
self.label = QtGui.QLabel("This is a test label", self)
self.hBox = QtGui.QHBoxLayout(self)
self.hBox.addWidget(self.openBtn)
self.hBox.addWidget(self.label)
self.group = QtGui.QGroupBox("Style", self)
self.radioBtn1 = QtGui.QRadioButton("Button 1", self.group)
self.radioBtn2 = QtGui.QRadioButton("Button 2", self.group)
self.radioLayout = QtGui.QVBoxLayout(self.group)
self.radioLayout.addWidget(self.radioBtn1)
self.radioLayout.addWidget(self.radioBtn2)
self.group.setLayout(self.radioLayout)
self.hBox2 = QtGui.QHBoxLayout(self)
self.hBox2.addWidget(self.group)
self.vBox = QtGui.QVBoxLayout(self)
self.vBox.addLayout(self.hBox2)
self.vBox.addLayout(self.hBox)
self.setLayout(self.vBox)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
mainapp = Application()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这是
我不明白为什么QGroupBox
会坏掉。它使用以下代码自行正确显示
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Application(QtGui.QWidget):
""" Main application window """
def __init__(self):
super(Application, self).__init__()
self.initUI()
def initUI(self):
# self.openBtn = QtGui.QPushButton("Open", self)
# self.label = QtGui.QLabel("This is a test label", self)
#
# self.hBox = QtGui.QHBoxLayout(self)
# self.hBox.addWidget(self.openBtn)
# self.hBox.addWidget(self.label)
self.group = QtGui.QGroupBox("Style", self)
self.radioBtn1 = QtGui.QRadioButton("Button 1", self.group)
self.radioBtn2 = QtGui.QRadioButton("Button 2", self.group)
self.radioLayout = QtGui.QVBoxLayout(self.group)
self.radioLayout.addWidget(self.radioBtn1)
self.radioLayout.addWidget(self.radioBtn2)
self.group.setLayout(self.radioLayout)
self.hBox2 = QtGui.QHBoxLayout(self)
self.hBox2.addWidget(self.group)
self.vBox = QtGui.QVBoxLayout(self)
self.vBox.addLayout(self.hBox2)
# self.vBox.addLayout(self.hBox)
self.setLayout(self.vBox)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
mainapp = Application()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
产生
我运行安装了 Python 3.3 的 Windows 8 64 位机器。