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