QT
编程模板:
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("软件名称")
self.resize(600,500)
self.func_list()
#调用super 类初始化继承
#调用方法集合
def func_list(self):
self.func()
self.func1()
self.func2()
#调用方法,形成方法集合
def func(self):
btn = QPushButton(self)
btn.setText("按钮")
btn.resize(120,30)
btn.move(100,100)
btn.setStyleSheet('font-size:20px')
def func1(self):
lable = QLabel(self)
lable.setText("标签")
lable.setStyleSheet('font-size:16px;')
def func2(self):
label2 = QLabel(self)
label2.setText('标签')
label2.move(200,300)
label2.setStyleSheet('font-size:16px;')
if __name__ =='__main__':
app =QApplication(sys.argv)
win= Window()
win.show()
sys.exit(app.exec_()) //循环调用保持方法。
删除OBJ 对象
代码举例
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("软件名称")
self.resize(600,500)
self.func_list()
def func_list(self):
self.func()
def func(self):
obj1 = QObject()
# obj1 转换为全局变量,self 指的是窗体
self.obj1 = obj1
obj2 = QObject()
obj3= QObject()
obj2.setParent(obj1)
obj3.setParent(obj2)
print(obj1)
print(obj2)
print(obj3)
obj1.destroyed.connect(lambda :print('obj1,释放'))
obj2.destroyed.connect(lambda: print('obj2,释放'))
l1 = QLabel(self)
l1.setText('l1')
l1.move(100,100)
l2 = QLabel(self)
l2.setText('l2')
l2.move(200,200)
# deletelater 是代码执行完成后执行, 无论它在哪一行
l2.deleteLater()
#del obj2
print(obj1.deleteLater())
print(obj1.children())
if __name__ =='__main__':
app =QApplication(sys.argv)
win= Window()
win.show()
sys.exit(app.exec_())