pyqt5- qgridlayout中的中心对象
我有一个问题。我尝试了PYQT5中的布局,它可以部分工作。但不是我想要的方式。
这是我的代码:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class CompanyLogin(QWidget):
def __init__(self, *args, **kwargs):
super().__init__()
self.layout = QGridLayout()
self.setLayout(self.layout)
self.setWindowTitle('Window')
self.setGeometry(0, 0, 1920, 1080)
self.setupUI()
def setupUI(self):
company_note = QLabel(parent=self, text='Company: ')
self.company = QComboBox(parent=self)
self.company.addItem('t1')
self.company.addItem('t2')
password_note = QLabel(parent=self, text='Password: ')
self.password = QLineEdit(parent=self)
submit = QPushButton(parent=self, text='Submit')
verticalSpacer = QSpacerItem(40, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.layout.addItem(verticalSpacer, 6, 0, Qt.AlignTop)
self.layout.addWidget(company_note, 1, 0, Qt.AlignLeft)
self.layout.addWidget(self.company, 1, 1, Qt.AlignRight)
self.layout.addWidget(password_note, 2, 0, Qt.AlignLeft)
self.layout.addWidget(self.password, 2, 1, Qt.AlignRight)
self.layout.addWidget(submit, 3, 0, 1, 0, Qt.AlignCenter)
这是我窗口到目前为止的样子:
,这就是我希望它看起来的样子:
aff
I have a question. I have tried layouts in PyQt5 and it works partially. But not the way I want it.
This is my code:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class CompanyLogin(QWidget):
def __init__(self, *args, **kwargs):
super().__init__()
self.layout = QGridLayout()
self.setLayout(self.layout)
self.setWindowTitle('Window')
self.setGeometry(0, 0, 1920, 1080)
self.setupUI()
def setupUI(self):
company_note = QLabel(parent=self, text='Company: ')
self.company = QComboBox(parent=self)
self.company.addItem('t1')
self.company.addItem('t2')
password_note = QLabel(parent=self, text='Password: ')
self.password = QLineEdit(parent=self)
submit = QPushButton(parent=self, text='Submit')
verticalSpacer = QSpacerItem(40, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.layout.addItem(verticalSpacer, 6, 0, Qt.AlignTop)
self.layout.addWidget(company_note, 1, 0, Qt.AlignLeft)
self.layout.addWidget(self.company, 1, 1, Qt.AlignRight)
self.layout.addWidget(password_note, 2, 0, Qt.AlignLeft)
self.layout.addWidget(self.password, 2, 1, Qt.AlignRight)
self.layout.addWidget(submit, 3, 0, 1, 0, Qt.AlignCenter)
This is what my window looks like so far:
And this is how I want it to look:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前,最简单的解决方案 是添加以下简单的行:
但是,实际上,设置布局对齐通常是导致意外问题的原因,事实是,您的代码中存在一些问题取而代之的是:
setRowsTretch()
setColumnStretch()
;其他小笔记:
layout()
< < /a>是所有qwidgets的现有动态函数,您不得覆盖它;另外,如果您然后将小部件添加到父级的布局中,则小部件构造函数中的parent
参数是毫无意义的。最后,仅在需要时使用关键字参数。Right now the simplest solution could be to add this simple line:
But, actually, setting the layout alignment is often cause of unexpected problems, and the fact is that there are some issues in your code that should be fixed instead:
setRowStretch()
andsetColumnStretch()
;Other small notes:
layout()
is an existing dynamic function of all QWidgets, you shall not overwrite it; also, theparent
argument in widget constructors is pointless if you then add the widget to the parent's layout; finally, use keyword arguments only when required.