【发布时间】:2022-01-07 10:09:36
【问题描述】:
我正在使用 PyQt5 为我的用户名、密码标签和输入字段以及我的提交和取消按钮创建一个网格布局。
我为我的所有小部件添加了相等的行宽 1,但是对于我的取消和提交按钮,我希望它们具有相同的单元格宽度。
这是我尝试过的
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
import sys
class MainWindow(qtw.QWidget):
def __init__(self, *arg, **kwargs):
super().__init__(*arg, **kwargs)
# custom code goes here
username_label = qtw.QLabel('Username')
password_label = qtw.QLabel('Password')
username_input = qtw.QLineEdit()
password_input = qtw.QLineEdit(echoMode = qtw.QLineEdit.Password)
cancel_button = qtw.QPushButton('Cancel')
submit_button = qtw.QPushButton('Login')
layout = qtw.QGridLayout()
layout.addWidget(username_label,0, 0, 1, 2) # 0,0 cell, rowspan : 1, colSpan : 2
layout.addWidget(username_input, 0, 1, 1, 3) # 0, 1 cell
layout.addWidget(password_label, 1, 0, 1, 2) # 1, 0 cell
layout.addWidget(password_input, 1, 1, 1, 3) # 1, 1 cell
layout.addWidget(cancel_button, 2, 0, 1)
layout.addWidget(submit_button, 2, 1, 1)
self.setLayout(layout)
self.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())
但是这给了我
如我所料,取消并登录以具有相等的宽度。
【问题讨论】:
标签: python layout pyqt pyqt5 qgridlayout