构造函数:
QTimer(parent: QObject = None): 创建一个 QTimer 对象,可指定父对象。
属性:
interval: 获取或设置定时器的时间间隔(以毫秒为单位)。
isActive: 检查定时器是否正在运行。
方法:
start(interval: int = 0): 启动定时器,可指定时间间隔(若未指定则使用之前设置的间隔)。
stop(): 停止定时器。
setInterval(interval: int): 设置定时器的时间间隔。
setSingleShot(bool)设置定时器是否仅触发一次。
singleShot(): 执行一次
信号:
timeout: 当定时器超时时发射此信号。
'''
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.resize(600,400)
self.setWindowTitle("QTimer定时器控件")
self.label = QLabel(self)
self.label.setGeometry(30,30,30,30)
self.label.setStyleSheet("background-color:red")
self.btn_start = QPushButton("启动",self)
self.btn_stop = QPushButton("停止",self)
self.btn_start.move(30,300)
self.btn_stop.move(150,300)
self.timer = QTimer(self)
# self.timer.setInterval(100)
self.timer.timeout.connect(self.on_timer_slot)
# self.timer.setSingleShot(True)
QTimer.singleShot(2000,self.label,self.change_label_color)
self.num =0
#slot
self.btn_start.clicked.connect(self.on_btn_start_click)
self.btn_stop.clicked.connect(self.on_btn_stop_click)
def change_label_color(self):
self.label.setStyleSheet("background-color:green")
def on_btn_start_click(self):
if self.timer.isActive():
return
self.timer.start(500)
def on_btn_stop_click(self):
if self.timer.isActive():
self.timer.stop()
def on_timer_slot(self):
print("我被执行了")
self.label.move(30+self.num,30)
self.num+=10
if self.num>self.width():self.num =0
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())


承担因您的行为而导致的法律责任,
本站有权保留或删除有争议评论。
参与本评论即表明您已经阅读并接受
上述条款。