我正在用PyQt4制作一个应用程序,这是我目前的代码:
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initUi()
def initUi(self):
self.setWindowTitle('Main Menu')
self.setFixedSize(1200, 625)
self.firstWidgets()
self.show()
def firstWidgets(self):
self.vbox1 = QtGui.QVBoxLayout()
self.task1 = QtGui.QLabel('Check 1', self)
self.task1CB = QtGui.QCheckBox(self)
self.hbox1 = QtGui.QHBoxLayout()
self.hbox1.addWidget(self.task1)
self.hbox1.addWidget(self.task1CB)
self.vbox1.addLayout(self.hbox1)
self.setLayout(self.vbox1)
def main():
application = QtGui.QApplication(sys.argv)
gui = MainWindow()
sys.exit(application.exec_())
if __name__=='__main__':
main()
我的问题在{.setLayout
作为该表单,这让我感到困惑
QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
只是更新布伦登·阿贝尔的答案:
QWidget和QVBoxLayout(对于Python3,PyQt5)现在包含在PyQt5.qtwidts模块中,而不是PyQt5.QtGui模块中
因此,更新代码:
这是一个使用PyQt5的示例
不能直接在
QMainWindow
上设置QLayout
。您需要创建一个QWidget
,并将其设置为QMainWindow
上的中心小部件,然后将QLayout
分配给该小部件相关问题 更多 >
编程相关推荐