zoukankan      html  css  js  c++  java
  • PyQt5Day06--事件处理+定时器

    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_())
    View Code

    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 # *******************案例**********************结束
    View Code

    时间:2020-03-03   19:14:43

    作者:931935931(QQ)

  • 相关阅读:
    day 30 粘包 自定义报头
    day29 网络基础之网络协议和通信
    day28 面向对象的进阶 反射 和类的内置方法
    day 27 模块和包 面向对象的复习
    CGI,FastCGI,PHP-CGI和PHP-FPM的区别
    跨平台的移动应用开发引擎CrossApp简介
    element-ui组件中的select等的change事件中传递自定义参数
    关于setInterval和setTImeout中的this指向问题
    懒加载和预加载的区别
    vueX的五个核心属性
  • 原文地址:https://www.cnblogs.com/fengxb1213/p/12404067.html
Copyright © 2011-2022 走看看