Q V B o x L a y o u t QVBoxLayout QVBoxLayout 和 Q H B o x L a y o u t QHBoxLayout QHBoxLayout 分别是垂直和水平布局管理器
QVBoxLayout
QVBoxLayout类垂直排列部件。
# -*- coding: utf-8 -*-
"""
Created on Fri May 8 23:20:44 2020
@author: Giyn
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
class Simple_Window(QWidget):
def __init__(self):
super(Simple_Window, self).__init__() # 使用super函数可以实现子类使用父类的方法
self.user = QLabel("用户名:", self) # self是指定的父类Simple_Window,表示QLabel属于Simple_Window窗口
self.pwd = QLabel("密码", self)
self.v_layout = QVBoxLayout() # 实例化一个垂直布局管理器
self.v_layout.addWidget(self.user) # 调用垂直布局管理器的addWidget方法来添加空间(注意顺序)
self.v_layout.addWidget(self.pwd)
self.setLayout(self.v_layout) # 调用窗口的setLayout方法将垂直布局设置为窗口的整体布局
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Simple_Window()
window.show()
sys.exit(app.exec())
Output:
QHBoxLayout
QHBoxLayout类水平排列部件。
# -*- coding: utf-8 -*-
"""
Created on Fri May 8 23:31:56 2020
@author: Giyn
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QLineEdit
class Simple_Window(QWidget):
def __init__(self):
super(Simple_Window, self).__init__() # 使用super函数可以实现子类使用父类的方法
self.input_label = QLabel("请输入:", self) # self是指定的父类Simple_Window,表示QLabel属于Simple_Window窗口
self.input_line = QLineEdit(self)
self.h_layout = QHBoxLayout() # 实例化一个水平布局管理器
self.h_layout.addWidget(self.input_label) # 调用水平布局管理器的addWidget方法来添加空间(注意顺序)
self.h_layout.addWidget(self.input_line)
self.setLayout(self.h_layout) # 调用窗口的setLayout方法将水平布局设置为窗口的整体布局
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Simple_Window()
window.show()
sys.exit(app.exec())