File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
05-QtWidgets-进阶话题/01-QLayout-布局管理器/05-QFormLayout-表单布局 Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ QFormLayout 表单布局
3
+
4
+ 表单布局专为「表单」使用场景设计优化,特点是有两列控件,左侧的为label标签,右侧的为field字段
5
+ 官方文档:https://doc.qt.io/qtforpython/PySide6/QtWidgets/QFormLayout.html
6
+ 继承自QLayout
7
+
8
+ 创建时可选传入父控件,(即使没有显式设置,也会在调用setLayout()方法时自动建立父子关系)
9
+ .__init__(self, parent: Optional[QWidget] = None)
10
+
11
+ """
12
+
13
+ import sys
14
+
15
+ from PySide6 import QtWidgets
16
+
17
+
18
+ class MyWidget (QtWidgets .QWidget ):
19
+ def __init__ (self , * args , ** kwargs ):
20
+ super ().__init__ (* args , ** kwargs )
21
+ self .setWindowTitle ("QFormLayout" )
22
+ self .setup_ui ()
23
+
24
+ def setup_ui (self ) -> None :
25
+ """设置界面"""
26
+
27
+ name_le = QtWidgets .QLineEdit (self )
28
+ age_label = QtWidgets .QLabel ("年龄:" , self )
29
+ age_spinbox = QtWidgets .QSpinBox (self )
30
+ age_spinbox .setRange (1 , 150 )
31
+ age_spinbox .setValue (20 )
32
+
33
+ layout = QtWidgets .QFormLayout (self )
34
+ layout .addRow ("姓名:" , name_le )
35
+ layout .addRow (age_label , age_spinbox )
36
+ self .setLayout (layout )
37
+
38
+
39
+ if __name__ == "__main__" :
40
+ app = QtWidgets .QApplication (sys .argv )
41
+ window = MyWidget ()
42
+ window .show ()
43
+ sys .exit (app .exec ())
You can’t perform that action at this time.
0 commit comments