我想为我的窗口做这样的布局:
所以我尝试创建一个QHBoxLayout
布局放置 3 个按钮,并将其添加到QVBoxLayout
:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
title = QtGui.QLabel( 'Title' )
author = QtGui.QLabel( 'Author' )
other = QtGui.QLabel( 'Other' )
titleEdit = QtGui.QLineEdit()
horizontalLayout = QtGui.QHBoxLayout( self )
horizontalLayout.addWidget( title )
horizontalLayout.addWidget( author )
horizontalLayout.addWidget( other )
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addWidget( titleEdit )
verticalLayout.addWidget( horizontalLayout )
self.setLayout( verticalLayout )
self.setGeometry( 300, 300, 350, 300 )
self.setWindowTitle( 'Review' )
self.show()
def main():
app = QtGui.QApplication( sys.argv )
ex = Example()
sys.exit( app.exec_() )
if __name__ == '__main__':
main()
但它不接受另一种布局:
verticalLayout.addWidget( horizontalLayout )
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Qt.Alignment = 0): argument 1 has unexpected type 'QHBoxLayout'
如何做到这一点?
Update
By @G.M. https://stackoverflow.com/users/6371123/评论,使用addLayout()
我在控制台上收到此警告:
QLayout: Attempting to add QLayout "" to Example "", which already has a layout
QLayout::addChildLayout: layout "" already has a parent
QWidget::setLayout: Attempting to set QLayout "" on Example "", which already has a layout
但现在显示的窗口没有编辑框:
您在更新中显示的问题是因为您必须将自己作为您的父母而生成的,并且它被放置在该小部件中,一个简单的解决方案是更改为:
horizontalLayout = QtGui.QHBoxLayout()
完整代码:
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self)
title = QtGui.QLabel( 'Title' )
author = QtGui.QLabel( 'Author' )
other = QtGui.QLabel( 'Other' )
titleEdit = QtGui.QLineEdit()
horizontalLayout = QtGui.QHBoxLayout()
horizontalLayout.addWidget( title )
horizontalLayout.addWidget( author )
horizontalLayout.addWidget( other )
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addLayout( horizontalLayout )
verticalLayout.addWidget( titleEdit )
self.setLayout( verticalLayout )
self.setGeometry( 300, 300, 350, 300 )
self.setWindowTitle( 'Review' )
self.show()
def main():
app = QtGui.QApplication( sys.argv )
ex = Example()
sys.exit( app.exec_() )
另一个问题是你谈论按钮,此外该图显示了一个几乎正方形尺寸的小部件,我认为如果你想得到它你应该使用QTextEdit
.
完整代码:
#!/usr/bin/python
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
title = QtGui.QPushButton( 'Title' )
author = QtGui.QPushButton( 'Author' )
other = QtGui.QPushButton( 'Other' )
titleEdit = QtGui.QTextEdit()
horizontalLayout = QtGui.QHBoxLayout()
horizontalLayout.addWidget( title )
horizontalLayout.addWidget( author )
horizontalLayout.addWidget( other )
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addLayout( horizontalLayout )
verticalLayout.addWidget( titleEdit )
self.setLayout( verticalLayout )
self.setGeometry( 300, 300, 350, 300 )
self.setWindowTitle( 'Review' )
self.show()
def main():
app = QtGui.QApplication( sys.argv )
ex = Example()
sys.exit( app.exec_() )
if __name__ == '__main__':
main()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)