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 19:56:42 浏览: 66
你可以使用setSpacing方法来减小用户名和密码与用户框和密码框的距离。setSpacing方法可以设置布局内部控件之间的间距。代码示例如下:
```
# 创建一个垂直布局
layout = QVBoxLayout()
# 创建一个水平布局,用于放置用户名标签和编辑框
username_layout = QHBoxLayout()
username_layout.setSpacing(10) # 设置间距为10像素
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_layout.setSpacing(10) # 设置间距为10像素
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.addLayout(password_layout)
# 设置布局
self.setLayout(layout)
```
你可以根据需要调整间距的大小。
阅读全文