资源:Python-GUI-PyQ5总概述.xmind;QObject.xmind
一、isWidgetType()
(1)判断是否是控件类型
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.QObject类型判定()
def QObject类型判定(self):
obj=QObject()
w=QWidget()
btn=QPushButton()
label=QLabel()
objs=[obj,w,btn,label]
for o in objs:
print(o.isWidgetType())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
#返回
'''
False
True
True
True
'''
(2)判断是否继承自QWidget类的
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.QObject类型判定()
def QObject类型判定(self):
obj=QObject()
w=QWidget()
btn=QPushButton()
label=QLabel()
objs=[obj,w,btn,label]
for o in objs:
print(o.inherits("QWidget"))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
#返回
'''
False
True
True
True
'''
二、inherits(父类):一个对象是否继承(直接或者间接)自某个类
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.QObject类型判定()
def QObject类型判定(self):
obj=QObject()
w=QWidget()
btn=QPushButton()
label=QLabel()
objs=[obj,w,btn,label]
for o in objs:
print(o.inherits("QPushButton"))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
#返回
'''
False
False
True
False
'''