您的位置:首页 > 移动开发 > Objective-C

8.QObject 定时器

2019-05-08 18:30 1621 查看

[code]class MyObject(QObject):        # 继承QObject
def timeEvent(self, evt):        # 重写 timeEvent方法
print('1', evt)

app = QApplication(sys.argv)

window = QWidget()
window = setWindowTitle('QObject定时器的使用')
window.resize(500, 500)

obj = MyObject()
timer_id = obj.startTimer(1000)        # 执行事件

obj.killTimer(timer_id)

 

案例1

[code]class MyLabel(QLabel):
def __init__(self, *args, **kwargs):
super().__init__()

self.setText('10')
self.move(100, 100)
self.setStyleSheet('font-size: 22px;')
self.timer_id = self.startTimer(1000)

def timeEvent(self, *args, **kwargs):
print('XX')
current_sec = int(self.text())
current_sec -= 1

self.steText(str(current_sec))

if current_sec == 0:
print('stop')
self.killTimer(self.timer_id)

app = QAoolication(sys.argv)

window = QWidget()
window.setWindowTitle('QObject定时器的使用')
window.resize(500, 500)
label = MyLabel(window)
window.show()
sys.exit(app.exec_())
[code]class MyLabel(QLabel):
def __init__(self, *args, **kwargs):
super().__init__()

self.setText('10')
self.move(100, 100)
self.setStyleSheet('font-size: 22px;')
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 timeEvent(self, *args, **kwargs):
print('XX')
current_sec = int(self.text())
current_sec -= 1

self.steText(str(current_sec))

if current_sec == 0:
print('stop')
self.killTimer(self.timer_id)

app = QAoolication(sys.argv)

window = QWidget()
window.setWindowTitle('QObject定时器的使用')
window.resize(500, 500)
label = MyLabel(window)
label.setSec(5)
label.startMyTimer(2000)
window.show()
sys.exit(app.exec_())

案例2

[code]class MyWidget(QWidget):
def __init__(self, *args, **kwargs):
super().__init__()

self.setWindowTitle('QObject定时器的使用')
self.resize(500, 500)
self.timer_id = self.startTimer(1000)

def timeEvent(self, *args, **kwargs):
current_w = self.width()
current_h = self.height()
self.resize(current_w + 10, current_h + 10)

app = QAoolication(sys.argv)
window = Mywidget()
window.show()
sys.exit(app.exec_())

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: