layout = QVBoxLayout() username_layout = QHBoxLayout() username_label = QLabel("用户名:") self.username_edit = QLineEdit() self.username_edit.setFixedSize(300, 30) # 设置用户名框大小 self.username_edit.setPlaceholderText("请输入用户名") # 设置占位符文字 username_layout.addWidget(username_label) username_layout.addWidget(self.username_edit) username_layout.setAlignment(Qt.AlignCenter) # 设置居中对齐 password_layout = QHBoxLayout() password_label = QLabel("密码:") self.password_edit = QLineEdit() self.password_edit.setFixedSize(300, 30) # 设置密码框大小 self.password_edit.setPlaceholderText("请输入密码") # 设置占位符文字 self.password_edit.setEchoMode(QLineEdit.Password) # 设置密码输入框的显示模式 password_layout.addWidget(password_label) password_layout.addWidget(self.password_edit) password_layout.setAlignment(Qt.AlignCenter) # 设置居中对齐 怎么缩减用户名和密码与用户框和密码框的距离
时间: 2024-02-28 09:56:38 浏览: 100
Qt-class.rar_QT_QT 中文_qt-class
你可以使用QSpacerItem来缩减用户名和密码与用户框和密码框的距离。在两个水平布局中间添加一个QSpacerItem即可。代码示例如下:
```
# 创建一个垂直布局
layout = QVBoxLayout()
# 创建一个水平布局,用于放置用户名标签和编辑框
username_layout = QHBoxLayout()
username_label = QLabel("用户名:")
self.username_edit = QLineEdit()
self.username_edit.setFixedSize(300, 30)
self.username_edit.setPlaceholderText("请输入用户名")
username_layout.addWidget(username_label)
username_layout.addWidget(self.username_edit)
username_layout.setAlignment(Qt.AlignCenter)
# 在用户名布局和密码布局之间添加一个间隔
spacer_item = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
# 创建一个水平布局,用于放置密码标签和编辑框
password_layout = QHBoxLayout()
password_label = QLabel("密码:")
self.password_edit = QLineEdit()
self.password_edit.setFixedSize(300, 30)
self.password_edit.setPlaceholderText("请输入密码")
self.password_edit.setEchoMode(QLineEdit.Password)
password_layout.addWidget(password_label)
password_layout.addWidget(self.password_edit)
password_layout.setAlignment(Qt.AlignCenter)
# 将布局添加到垂直布局中
layout.addLayout(username_layout)
layout.addItem(spacer_item)
layout.addLayout(password_layout)
# 设置布局
self.setLayout(layout)
```
阅读全文