pyqt5-布局控件
Posted liming19680104
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt5-布局控件相关的知识,希望对你有一定的参考价值。
在布局中添加控件用addWidght(),添加布局用addLayout()
垂直布局QVBoxLayout
需要导入 from PyQt5.QtWidgets import QVBoxLayout
1 import sys 2 from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout 3 4 5 class Demo(QWidget): 6 7 def __init__(self): 8 super(Demo, self).__init__() 9 self.user_label = QLabel(‘用户名字:‘, self) 10 self.pwd_label = QLabel(‘密码:‘, self) 11 12 self.v_layout = QVBoxLayout() #实例化一个垂直布局管理器 13 self.v_layout.addWidget(self.user_label) #将控件添加到垂直布局中,最先添加的出现在最上方 14 self.v_layout.addWidget(self.pwd_label) 15 16 self.setLayout(self.v_layout) #将self.v_layout设为整个窗口的最终布局方式 17 18 19 if __name__ == ‘__main__‘: 20 app = QApplication(sys.argv) 21 demo = Demo() 22 demo.show() 23 sys.exit(app.exec_())
水平布局QHBoxLayout
需要 from PyQt5.QtWidgets import QHBoxLayout
1 import sys 2 from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QHBoxLayout 3 4 class Demo(QWidget): 5 6 def __init__(self): 7 super(Demo, self).__init__() 8 self.user_label = QLabel(‘Username:‘, self) 9 self.user_line = QLineEdit(self) #实例化一个单行文本输入框 10 11 self.h_layout = QHBoxLayout() #实例化一个水平布局管理器 12 self.h_layout.addWidget(self.user_label) #将控件添加到水平布局管理器中,先添加的出现在左边 13 self.h_layout.addWidget(self.user_line) 14 15 self.setLayout(self.h_layout) 16 17 18 if __name__ == ‘__main__‘: 19 app = QApplication(sys.argv) 20 demo = Demo() 21 demo.show() 22 sys.exit(app.exec_())
效果图:
混合使用QVBoxLayout和QHBoxLayout
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton,
QHBoxLayout, QVBoxLayout
class Demo(QWidget):
def __init__(self):
super(Demo, self).__init__()
self.user_label = QLabel(‘Username:‘, self)
self.pwd_label = QLabel(‘Password:‘, self)
self.user_line = QLineEdit(self)
self.pwd_line = QLineEdit(self)
self.login_button = QPushButton(‘登陆, self)
self.signin_button = QPushButton(‘注册‘, self)
self.label_v_layout = QVBoxLayout()
self.line_v_layout = QVBoxLayout()
self.button_h_layout = QHBoxLayout()
self.label_line_h_layout = QHBoxLayout()
self.all_v_layout = QVBoxLayout()
self.label_v_layout.addWidget(self.user_label)
self.label_v_layout.addWidget(self.pwd_label)
self.line_v_layout.addWidget(self.user_line)
self.line_v_layout.addWidget(self.pwd_line)
self.button_h_layout.addWidget(self.login_button)
self.button_h_layout.addWidget(self.signin_button)
self.label_line_h_layout.addLayout(self.label_v_layout) #在布局中添加布局
self.label_line_h_layout.addLayout(self.line_v_layout)
self.all_v_layout.addLayout(self.label_line_h_layout)
self.all_v_layout.addLayout(self.button_h_layout)
self.setLayout(self.all_v_layout)
if __name__ == ‘__main__‘:
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())
效果图:
表单布局QFormLayout
表单布局可以将控件以两列的形式进行排布,左列控件为文本标签,右列为输入型的控件
1 import sys 2 from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, 3 QHBoxLayout, QVBoxLayout, QFormLayout 4 5 class Demo(QWidget): 6 7 def __init__(self): 8 super(Demo, self).__init__() 9 10 self.user_label = QLabel(‘姓名:‘, self) 11 self.pwd_label = QLabel(‘密码:‘, self) 12 self.user_line = QLineEdit(self) 13 self.pwd_line = QLineEdit(self) 14 self.login_button = QPushButton(‘登陆‘, self) 15 self.signin_button = QPushButton(‘注册‘, self) 16 17 self.f_layout = QFormLayout() #实例化一个QFormLayout控件 18 self.button_h_layout = QHBoxLayout() #用来布局按钮 19 self.all_v_layout = QVBoxLayout() #最终布局 20 21 self.f_layout.addRow(self.user_label, self.user_line) # 在表单布局中添加控件 22 self.f_layout.addRow(self.pwd_label, self.pwd_line) 23 self.button_h_layout.addWidget(self.login_button) 24 self.button_h_layout.addWidget(self.signin_button) 25 self.all_v_layout.addLayout(self.f_layout) 26 self.all_v_layout.addLayout(self.button_h_layout) 27 28 self.setLayout(self.all_v_layout) 29 30 31 if __name__ == ‘__main__‘: 32 app = QApplication(sys.argv) 33 demo = Demo() 34 demo.show() 35 sys.exit(app.exec_())
网格布局QGridLayout
当使用该布局管理器的时候,你可以把整个窗体想象成带有格子的,然后只要把各个控件放在相应的格子就好了
需要 from PyQt5.QtWidgets import QGridLayout
1 import sys 2 from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, 3 QGridLayout, QVBoxLayout, QHBoxLayout 4 5 6 class Demo(QWidget): 7 8 def __init__(self): 9 super(Demo, self).__init__() 10 11 self.user_label = QLabel(‘姓名:‘, self) 12 self.pwd_label = QLabel(‘密码:‘, self) 13 self.user_line = QLineEdit(self) 14 self.pwd_line = QLineEdit(self) 15 self.login_button = QPushButton(‘登陆‘, self) 16 self.signin_button = QPushButton(‘注册‘, self) 17 18 self.grid_layout = QGridLayout() # 实例化一个QGridLayout布局管理器 19 self.h_layout = QHBoxLayout() 20 self.v_layout = QVBoxLayout() 21 22 self.grid_layout.addWidget(self.user_label, 0, 0, 1, 1) #在网格布局中添加控件 23 #遵循格式:addWidget(widget, row, column, rowSpan, columnSpan) 24 #widget就是要添加的控件 25 #row为第几行,0代表第一行;column为第几列,0代表第一列 26 #rowSpan表示要让这个控件去占用几行(默认一行);columnSpan表示要让这个控件去占用几列(默认一列) 27 self.grid_layout.addWidget(self.user_line, 0, 1, 1, 1) 28 self.grid_layout.addWidget(self.pwd_label, 1, 0, 1, 1) 29 self.grid_layout.addWidget(self.pwd_line, 1, 1, 1, 1) 30 self.h_layout.addWidget(self.login_button) 31 self.h_layout.addWidget(self.signin_button) 32 self.v_layout.addLayout(self.grid_layout) 33 self.v_layout.addLayout(self.h_layout) 34 35 self.setLayout(self.v_layout) 36 37 38 if __name__ == ‘__main__‘: 39 app = QApplication(sys.argv) 40 demo = Demo() 41 demo.show() 42 sys.exit(app.exec_())
以上是关于pyqt5-布局控件的主要内容,如果未能解决你的问题,请参考以下文章
python3 PyQt5 运行后界面控件缩在左上角,但在QTdesigner里面预览是正常的?