基本使用
from PyQt5.Qt import *
import sys
# 新建应用
app = QApplication(sys.argv)
# 创建控件
window = QWidget()
# 设置控件
window.setWindowTitle('111')
window.resize(500, 500)
window.move(400, 200)
label = QLabel(window)
label.setText('ashdjka')
label.move(200, 200)
# 展示控件
window.show()
# 事件循环
sys.exit(app.exec_())
通过类封装和样式修饰
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ashdjhj')
self.resize(500, 500)
self.set_ui()
def set_ui(self):
self.QObject_struct()
self.QObject_name_attr()
self.QObject_demo()
self.QObject_p_s()
# QObject继承结构
def QObject_struct(self):
mros = QObject.mro()
for mro in mros:
print(mro)
def QObject_name_attr(self):
obj = QObject()
# 设置Qt对象名
obj.setObjectName('notice')
print(obj.objectName())
# 给一个Qt对象动态添加一个属性与值
obj.setProperty('notice_level', 'error')
obj.setProperty('notice_level2', 'warning')
# 获取一个对象的属性值
print(obj.property('notice_level'))
# 获取一个对象通过setProperty设置的所有值
print(obj.dynamicPropertyNames())
def QObject_demo(self):
with open('../qss/QObject.qss', 'r') as f:
qApp.setStyleSheet(f.read())
label = QLabel(self)
label.setText('hello world')
label.setObjectName('notice')
label.setProperty('notice_level', 'normal')
# style 方式调账样式
# label.setStyleSheet("font-size:20px; color:red;")
label2 = QLabel(self)
label2.setText('label2')
label2.move(100, 100)
label2.setObjectName('notice')
label2.setProperty('notice_level', 'warning')
label3 = QLabel(self)
label3.setText('label3')
label3.move(200, 200)
label3.setObjectName('notice')
label3.setProperty('notice_level', 'error')
def QObject_p_s(self):
# obj0 = QObject()
# obj1 = QObject()
# obj2 = QObject()
# obj3 = QObject()
# obj4 = QObject()
# obj5 = QObject()
# print('obj0', obj0)
# print('obj1', obj1)
# print('obj2', obj2)
# print('obj3', obj3)
# print('obj4', obj4)
# print('obj5', obj5)
#
# obj1.setParent(obj0)
# obj2.setParent(obj0)
# obj3.setParent(obj1)
# obj4.setParent(obj2)
# obj5.setParent(obj2)
# print(obj0.parent())
# print(obj1.parent())
# print(obj2.parent())
# print(obj3.parent())
# print(obj4.parent())
# print(obj5.parent())
#
# print(obj0.children())
#
# # findChild方法有三个参数,参数1 为需要查找的子对象类型,参数2 为子对象名称, 参数3 为查找选项 Qt.FindChildrenRecursively 递归查找,默认选项。 Qt.FindDirectChlidrenOnly 直接子对象
# print(obj0.findChild(QObject))
# print(obj0.findChildren(QObject))
# 内存管理,下列代码执行完毕后,由于obj0未被使用,被自动释放,obj1为其子节点,也同样被释放
obj0 = QObject()
obj1 = QObject()
obj1.setParent(obj0)
obj1.destroyed.connect(lambda : print('obj1 被释放'))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
样式代码QObject.qss:
QLabel#notice {
font-size:20px;
color:gray;
border: 1px solid gray;
border-radius: 8px;
}
QLabel#notice[notice_level='normal'] {
color: green;
border-color: green;
}
QLabel#notice[notice_level='warning'] {
color: yellow;
border-color: yellow;
}
QLabel#notice[notice_level='error'] {
color: red;
border-color: red;
}
示例
pyqt_案例01-两个独立窗口.py
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ashdjhj')
self.resize(500, 500)
self.set_ui()
def set_ui(self):
self.QObject_struct()
self.QObject_name_attr()
self.QObject_demo()
self.QObject_p_s()
# QObject继承结构
def QObject_struct(self):
mros = QObject.mro()
for mro in mros:
print(mro)
def QObject_name_attr(self):
obj = QObject()
# 设置Qt对象名
obj.setObjectName('notice')
print(obj.objectName())
# 给一个Qt对象动态添加一个属性与值
obj.setProperty('notice_level', 'error')
obj.setProperty('notice_level2', 'warning')
# 获取一个对象的属性值
print(obj.property('notice_level'))
# 获取一个对象通过setProperty设置的所有值
print(obj.dynamicPropertyNames())
def QObject_demo(self):
with open('../qss/QObject.qss', 'r') as f:
qApp.setStyleSheet(f.read())
label = QLabel(self)
label.setText('hello world')
label.setObjectName('notice')
label.setProperty('notice_level', 'normal')
# style 方式调账样式
# label.setStyleSheet("font-size:20px; color:red;")
label2 = QLabel(self)
label2.setText('label2')
label2.move(100, 100)
label2.setObjectName('notice')
label2.setProperty('notice_level', 'warning')
label3 = QLabel(self)
label3.setText('label3')
label3.move(200, 200)
label3.setObjectName('notice')
label3.setProperty('notice_level', 'error')
def QObject_p_s(self):
# obj0 = QObject()
# obj1 = QObject()
# obj2 = QObject()
# obj3 = QObject()
# obj4 = QObject()
# obj5 = QObject()
# print('obj0', obj0)
# print('obj1', obj1)
# print('obj2', obj2)
# print('obj3', obj3)
# print('obj4', obj4)
# print('obj5', obj5)
#
# obj1.setParent(obj0)
# obj2.setParent(obj0)
# obj3.setParent(obj1)
# obj4.setParent(obj2)
# obj5.setParent(obj2)
# print(obj0.parent())
# print(obj1.parent())
# print(obj2.parent())
# print(obj3.parent())
# print(obj4.parent())
# print(obj5.parent())
#
# print(obj0.children())
#
# # findChild方法有三个参数,参数1 为需要查找的子对象类型,参数2 为子对象名称, 参数3 为查找选项 Qt.FindChildrenRecursively 递归查找,默认选项。 Qt.FindDirectChlidrenOnly 直接子对象
# print(obj0.findChild(QObject))
# print(obj0.findChildren(QObject))
# 内存管理,下列代码执行完毕后,由于obj0未被使用,被自动释放,obj1为其子节点,也同样被释放
obj0 = QObject()
obj1 = QObject()
obj1.setParent(obj0)
obj1.destroyed.connect(lambda : print('obj1 被释放'))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
pyqt_案例02-独立窗口父子关系.py
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ashdjhj')
self.resize(500, 500)
self.set_ui()
def set_ui(self):
pass
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = QWidget()
window2 = QWidget()
window3 = QWidget()
window.setWindowTitle('window')
window2.setWindowTitle('window2')
window3.setWindowTitle('window3')
window2.setParent(window)
window3.setParent(window)
window2.setStyleSheet('background-color: red;')
window3.setStyleSheet('background-color: green;')
window2.resize(100,100)
# window3会将window2覆盖
window3.resize(200,100)
window.show()
sys.exit(app.exec_())
pyqt_案例03-统一Qlabel样式.py
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ashdjhj')
self.resize(500, 500)
self.set_ui()
def set_ui(self):
pass
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = QWidget()
label1 = QLabel(win)
label1.setText('label1')
label2 = QLabel(win)
label2.setText('label2')
label2.move(50, 50)
btn = QPushButton(win)
btn.setText('btn')
btn.move(100, 100)
win.show()
# for sub_widget in win.children():
# print(sub_widget)
for sub_widget in win.findChildren(QLabel):
print(sub_widget)
sub_widget.setStyleSheet('background-color:cyan;')
sys.exit(app.exec_())