法
QWidget
按钮
btn = QPushButton("按钮")
按钮触发器
btn.clicked.connect(引用地址不要括号)
单选按钮
rbtn = QRadioButton("单选按钮")
标签
label = QLabel("标签: ")
输入框
#输入框
edit = QLineEdit(父亲谁)
edit.setPlaceholderText("占空位置")
组
hobby_box = QGroupBox("爱好")
布局
盒子布局
垂直布局
layout = QVBoxLayout()
伸展器(弹簧)
layout.addStretch(1)
垂直布局案例
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QPushButton, QGroupBox, QMainWindow
from PyQt5.QtCore import Qt
class MyWindow(QWidget):
def __init__(self):
# 切记一定要调用父类的__init__方法,因为它里面有很多对UI空间的初始化操作
super().__init__()
# 设置大小
self.resize(300, 300)
# 设置标题
self.setWindowTitle("垂直布局")
# 垂直布局
layout = QVBoxLayout()
# 作用是在布局器中增加一个伸缩量,里面的参数表示QSpacerItem的个数,默认值为零
# 会将你放在layout中的空间压缩成默认的大小
# 下面的笔试1:1:1:2
layout.addStretch(1)
# 按钮1
btn1 = QPushButton("按钮1")
# 添加到布局器中
# layout.addWidget(btn1, Qt.AlignmentFlag.AlignTop)
layout.addWidget(btn1)
layout.addStretch(1)
# 按钮2
btn2 = QPushButton("按钮2")
# 添加到布局器
layout.addWidget(btn2)
layout.addStretch(1)
# 按钮3
btn3 = QPushButton("按钮3")
# 添加到布局器
layout.addWidget(btn3)
layout.addStretch(2)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
# 创建一个QWidget子类
w = MyWindow()
w.show()
app.exec()
水平布局
h_layout = QHBoxLayout()
水平布局案例(布局嵌套)
import sys
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(300, 300)
vertical = QVBoxLayout()
hobby_box_layout = QVBoxLayout()
hobby_box = QGroupBox("爱好")
btn1 = QPushButton("抽烟")
btn2 = QPushButton("喝酒")
btn3 = QPushButton("开发")
hobby_box_layout.addWidget(btn1)
hobby_box_layout.addWidget(btn2)
hobby_box_layout.addWidget(btn3)
hobby_box.setLayout(hobby_box_layout)
# ----------------------------one
gender_box = QGroupBox("性别")
gender_box_layout = QHBoxLayout()
rbtn_1 = QRadioButton("男")
rbtn_2 = QRadioButton("女")
gender_box_layout.addWidget(rbtn_1)
gender_box_layout.addWidget(rbtn_2)
gender_box.setLayout(gender_box_layout)
vertical.addWidget(hobby_box)
vertical.addWidget(gender_box)
self.setLayout(vertical)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec()
网格布局
声明网格布局
grid = QGridLayout()
网格布局案例
import sys
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super().__init__()
container = QVBoxLayout()
data = {
0: {"7", "8", "9", "+"},
1: {"4", "5", "6", "-"},
2: {"3", "2", "1", "*"},
3: {"0", ".", "=", "/"}
}
edit = QLineEdit()
edit.setPlaceholderText("请输入内容")
container.addWidget(edit)
grid = QGridLayout()
for line_number, line_data in data.items():
for clo_number, number in enumerate(line_data):
btn = QPushButton(number)
grid.addWidget(btn, line_number, clo_number)
#///布局可以添加布局
container.addLayout(grid)
self.setLayout(container)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec()
全屏居中 小图标
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle("王明东基础Qt入门UI")
# 窗口的大小
w.resize(300, 300)
# 锁死窗口大小
# w.setFixedSize(300, 300)
# 调整窗口在屏幕中央显示
center_pointer = QDesktopWidget().availableGeometry().center()
x = center_pointer.x()
y = center_pointer.y()
# w.move(x, y)
# w.move(x-150, y-150)
print(w.frameGeometry())
print(w.frameGeometry().getRect())
print(type(w.frameGeometry().getRect()))
old_x, old_y, width, height = w.frameGeometry().getRect()
# 使用move时候需要整数int类型
w.move(x - width // 2, y - height // 2)
# 设置图标需要引用
w.setWindowIcon(QIcon('panda.png'))
# 按钮
btn = QPushButton("直接填写按钮名称")
# 设置其父亲是谁
btn.setParent(w)
# 文本
# 下面创建一个Label,然后调用方法指定父类
lab = QLabel("文本", w)
# 设置组件位置(x, y, w, h)
lab.setGeometry(20, 20, 30, 30)
# 输入框
edit = QLineEdit(w)
edit.setPlaceholderText("占空符")
edit.setGeometry(20, 50, 100, 30)
# 最好最后执行 在它之后添加的元素是不会展示出来的
w.show()
app.exec()
表单布局 (登录窗口)
表单布局
form_layout = QFormLayout()
添加Qwidget小案例
edit = QLineEdit()
edit.setPlaceholderText("请输入账号")
form_layout.addRow("账号:", edit)
表单布局案例
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QVBoxLayout, QFormLayout, QLineEdit, QPushButton, QApplication, QWidget
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
# 设定当前Widget的宽高(可以拉伸大小)
# self.resize(300, 200)
# 禁止改变宽高(不可以拉伸)
self.setFixedSize(300, 150)
# 外层容器
container = QVBoxLayout()
# 表单容器
form_layout = QFormLayout()
# 创建1个输入框
edit = QLineEdit()
edit.setPlaceholderText("请输入账号")
form_layout.addRow("账号:", edit)
# 创建另外1个输入框
edit2 = QLineEdit()
edit2.setPlaceholderText("请输入密码")
form_layout.addRow("密码:", edit2)
# 将from_layout添加到垂直布局器中
container.addLayout(form_layout)
# 按钮
login_btn = QPushButton("登录")
login_btn.setFixedSize(100, 30)
# 把按钮添加到容器中,并且指定它的对齐方式
container.addWidget(login_btn, alignment=Qt.AlignRight)
# 设置当前Widget的布局器,从而显示这个布局器中的子控件
self.setLayout(container)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec()
抽屉布局
声明抽屉样式
self.stacked_layout = QStackedLayout()
设置显示那个面板
self.stacked_layout.setCurrentIndex(0)
抽屉布局案例
import sys
from PyQt5.QtWidgets import *
class Wind1(QWidget):
def __init__(self):
super().__init__()
QLabel("我是面板1", self)
class Wind2(QWidget):
def __init__(self):
super().__init__()
QLabel("我是面板2", self)
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(300, 300)
self.stacked_layout = QStackedLayout()
self.create_widget()
self.init_ui()
def create_widget(self):
win1 = Wind1()
win2 = Wind2()
self.stacked_layout.addWidget(win1)
self.stacked_layout.addWidget(win2)
def init_ui(self):
container = QVBoxLayout()
widget = QWidget()
widget.setLayout(self.stacked_layout)
btn1 = QPushButton("这是按钮1")
btn2 = QPushButton("这是按钮2")
btn1.clicked.connect(self.show_win1)
btn2.clicked.connect(self.show_win2)
container.addWidget(widget)
container.addWidget(btn1)
container.addWidget(btn2)
self.setLayout(container)
def show_win1(self):
print("面板1")
self.stacked_layout.setCurrentIndex(0)
def show_win2(self):
print("面板2")
self.stacked_layout.setCurrentIndex(1)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec()