1、事件处理
(1)框架
(2)操作练习
1 import sys 2 from PyQt5.Qt import * 3 4 class App(QApplication): 5 def notify(self,recevier,evt): 6 if recevier.inherits("QPushButton") and evt.type()==QEvent.MouseButtonPress: 7 print(recevier,evt) 8 return super().notify(recevier,evt) 9 10 class Btn(QPushButton): 11 def event(self,evt): 12 if evt.type() == QEvent.MouseButtonPress: 13 print(evt) 14 return super().event(evt) 15 16 def mousePressEvent(self, *args, **kwargs): 17 print("鼠标被按下了。。。。。。") 18 return super().mousePressEvent(*args, **kwargs) 19 20 app=App(sys.argv) 21 22 window=QWidget() 23 24 btn=Btn(window) 25 btn.setText('按钮') 26 btn.move(100,100) 27 28 def cao(): 29 print("按钮被点击了") 30 31 btn.pressed.connect(cao) 32 33 window.show() 34 sys.exit(app.exec_())
2、定时器
(1)框架
(2)API操作
1 # *******************定时器**********************开始 2 import sys 3 from PyQt5.Qt import * 4 5 class MyObject(QObject): # 重写QObject中的timerEvent 6 def timerEvent(self, evt): 7 print(evt,"1") 8 9 app=QApplication(sys.argv) 10 11 window=QWidget() 12 window.setWindowTitle("QObject定时器的使用") 13 window.resize(500,500) 14 15 obj=MyObject() 16 time_id=obj.startTimer(1000) # 每隔1000ms执行obj对象里的timerEvent 17 obj.killTimer(time_id) # 关闭定时器 18 19 window.show() 20 sys.exit(app.exec_()) 21 # *******************定时器**********************结束
(3)案例
1 # *******************案例**********************开始 2 import sys 3 from PyQt5.Qt import * 4 5 class MyQlabel(QLabel): 6 def __init__(self, *args, **kwargs): 7 super().__init__(*args, **kwargs) 8 self.setText("10") 9 self.move(225, 200) 10 self.setStyleSheet("font-size:50px;background-color:green") 11 self.timer_id = self.startTimer(1000) 12 13 def timerEvent(self, *args, **kwargs): 14 # 1.获取当前标签的内容 15 current_sec=int(self.text()) 16 current_sec-=1 17 self.setText(str(current_sec)) 18 if current_sec == 0: 19 print("停止") 20 self.killTimer(self.timer_id) 21 22 class MyWindow(QWidget): 23 def __init__(self,*args, **kwargs): 24 super().__init__(*args, **kwargs) 25 self.setWindowTitle("定时器案例") 26 self.resize(500, 500) 27 self.time_id=self.startTimer(100) 28 29 def timerEvent(self, *args, **kwargs): 30 current_w=self.width() 31 32 current_h=self.height() 33 self.resize(current_w+10,current_h+10) 34 if current_w == 650 and current_h == 650: 35 self.killTimer(self.time_id) 36 37 app=QApplication(sys.argv) 38 39 window=MyWindow() # 类的实例化,在类中完成相应功能 40 # label=MyQlabel(window) # 类的实例化,在类中完成相应功能 41 42 window.show() 43 sys.exit(app.exec_()) 44 # *******************案例**********************结束
时间:2020-03-03 19:14:43
作者:931935931(QQ)