from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习")
self.resize(500, 500)
self.setup_ui()
def setup_ui(self):
self.QObject类型判定()
def QObject类型判定(self):
# *************API***************开始
# obj = QObject()
# w = QWidget()
# btn = QPushButton()
# label = QLabel()
#
# objs = [obj, w, btn, label]
# for o in objs:
# print("是否是QWidget的控件类型",o.isWidgetType())
# print("父类是否是QWidget",o.inherits("QWidget"))
# print("父类是否是QPushButton",o.inherits("QPushButton"))
# print("------------------------------------------")
# *************API***************结束
# *************案例***************开始
label1 = QLabel(self)
label1.setText("景天科技苑")
label1.move(100, 100)
label2 = QLabel(self)
label2.setText("科技不间断")
label2.move(150, 150)
btn = QPushButton(self)
btn.setText("点我")
btn.move(200, 200)
#循环遍历子对象
# for widget in self.findChildren(QLabel):
for widget in self.children():
# print(widget)
# if widget.isWidgetType():
#将子对象中为QLabel的对象设置背景样式
if widget.inherits("QLabel"):
widget.setStyleSheet("background-color: cyan;")
# *************案例***************结束
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
|