pyqt5-005

时间处理:

  API:(监听,过滤一些底层事件)

    childEvent()

    customEvent()

    eventFilter()

    installEventFilter()

    removeEventFilter()

    event()

  应用场景:

    事件机制:

      相较于‘信号与槽’机制:

        信号与槽机制是对时间机制的高级封装

        事件机制更偏底层(远离用户)

    拦截事件,监听特定行为

 

# 事件机制演练

import sys
from PyQt5.Qt import *

# app = QApplication(sys.argv)

class App(QApplication):
    def notify(self, receiver, event):
        # 方法重写
        if receiver.inherits('QPushButton') and event.type() == QEvent.MouseButtonPress:
            # 过滤只有QPushButton按钮并且只有按下操作
            print(receiver, event)
        return super().notify(receiver, event)
        # 调用父类方法, 并且返回,负责分发信号


class Btn(QPushButton):
    def event(self, event):
        if event.type() == QEvent.MouseButtonPress:
            print('按钮点击了')
        return super().event(event)
    def mousePressEvent(self, *args, **kwargs):
        print('鼠标被按下了。。。。。。。')
        return super().mousePressEvent(*args, **kwargs)

app = App(sys.argv)

window = QWidget()


# btn = Btn(window)

btn = QPushButton(window)
btn.setText('按钮')
btn.move(100, 100)

def cao():
    print('按钮被点击了')

btn.pressed.connect(cao)

window.show()


sys.exit(app.exec_())

 

 

定时器:

  API:

    startTime(ms,QTimerType)-> timer_id:

      开启一个定时器

      Qt.TimeType:

        Qt.PreciseTimer =>精确定时器:尽可能保持毫秒精确

        Qt.CoarseTimer=>粗定时器:5%的毫秒误差间隔

        Qt.VeryCoarseTimer=>很粗的定时器:只能到秒级

      timer_id: 定时器的唯一标识

    killTimer(timer_id)  <根据定时器ID,杀死定时器>

    timerEvent() :定时器执行事件

      

 

# 定时器

from PyQt5.Qt import *
import sys

class MyObject(QObject):
    def timerEvent(self, event):
        print(event, '1')


class MyLabel(QLabel):
    def __init__(self, *args, **kwargs):
        super().__init__( *args, **kwargs)
        self.setText('10')
        self.setStyleSheet('font-size: 30px;color:green;')
        self.move(100, 100)

        # self.timer_id = self.startTimer(1000)

    def setSec(self, sec):
        # 自定义定时器时间
        self.setText(str(sec))

    def startMyTimer(self, ms):
        self.timer_id = self.startTimer(ms)

    def timerEvent(self, *args, **kwargs):
        # 自定义当前时间处理事件
        print('xx')
        current_sec = int(self.text())
        # 获取当前的标签
        current_sec -=1
        self.setText(str(current_sec))
        # 重设当前标签
        if current_sec ==0:
            print('停止')
            self.killTimer(self.timer_id)


class MyWidget(QWidget):
    def timerEvent(self, *args, **kwargs):
        current_w = self.width()
        current_h = self.height()
        self.resize(current_w + 10, current_h + 10)
        print('xxx')

app = QApplication(sys.argv)
# window = QWidget()
# window.resize(300,300)
# window.setWindowTitle('定时器')

# obj = MyObject()
# timer_id = obj.startTimer(1000)
# 定时器,单位毫秒
# obj.killTimer(timer_id)
# 杀死定时器



# 定时器控件
# label = MyLabel(window) #window为父控件
# label.setSec(5)
# label.startMyTimer(500)


# 窗口大小递增
window = MyWidget()
window.resize(300,300)
window.setWindowTitle('定时器')

window.startTimer(1000)



window.show()
sys.exit(app.exec_())

 

posted @ 2020-05-11 15:12  King~~~  阅读(193)  评论(0编辑  收藏  举报