我的想法是在中显示一个密码输入框QtGui MainWindow.在执行密码检查后,它将调用程序以显示其余按钮.我希望我的密码输入框是这样的

这是我的代码:
class MainWindow(QtGui.QMainWindow):
def __init__(self,parent = None):
super(MainWindow,self).__init__(parent)
self.CheckPassword()
def CheckPassword(self):
username_label = QtGui.QLabel("Username :")
password_label = QtGui.QLabel("Password :")
_username = QtGui.QLineEdit()
_password = QtGui.QLineEdit()
password_layout = QtGui.QGridLayout()
password_layout.addWidget(username_label,1,0)
password_layout.addWidget(password_label,2,0)
password_layout.addWidget(_username,1,1)
password_layout.addWidget(_password,2,1)
password_widget = QtGui.QWidget()
password_widget.setLayout(password_layout)
self.setCentralWidget(password_widget)
Run Code Online (Sandbox Code Playgroud)
我的问题是,因为我将窗口小部件设置为变得centralwidget像窗口一样大.
所以我尝试设置使用,setMaximumSize但在这种情况下,我似乎无法将小部件放在中心位置MainWindow.
如何为我的密码输入设置QWidget的大小,并将其定位为始终居中,无论主窗口的大小是多少?
您可以通过设置行和列拉伸来完成此操作.将小部件移动到网格的中心,在两侧留下一行和一列,然后为这些列设置拉伸因子.
网格的可视示例
+------------------------------+
| s stretch s |
| t +-------+-------+ t |
| r | label | input | r |
| e +-------+-------+ e |
| t | label | input | t |
| c +-------+-------+ c |
| h stretch h |
+------------------------------+
Run Code Online (Sandbox Code Playgroud)
码:
password_layout = QtGui.QGridLayout()
# Set the stretch
password_layout.setColumnStretch(0, 1)
password_layout.setColumnStretch(3, 1)
password_layout.setRowStretch(0, 1)
password_layout.setRowStretch(3, 1)
# Add widgets
password_layout.addWidget(username_label, 1, 1)
password_layout.addWidget(password_label, 2, 1)
password_layout.addWidget(_username, 1, 2)
password_layout.addWidget(_password, 2, 2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5695 次 |
| 最近记录: |