'''
構造函數(shù):
QTimer(parent: QObject = None): 創(chuàng)建一個 QTimer 對象,可指定父對象。
屬性:
interval: 獲取或設置定時器的時間間隔(以毫秒為單位)。
isActive: 檢查定時器是否正在運行。
方法:
start(interval: int = 0): 啟動定時器,可指定時間間隔(若未指定則使用之前設置的間隔)。
stop(): 停止定時器。
setInterval(interval: int): 設置定時器的時間間隔。
setSingleShot(bool)設置定時器是否僅觸發(fā)一次。
singleShot(): 執(zhí)行一次
信號:
timeout: 當定時器超時時發(fā)射此信號。
'''
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("我被執(zhí)行了")
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())
承擔因您的行為而導致的法律責任,
本站有權保留或刪除有爭議評論。
參與本評論即表明您已經閱讀并接受
上述條款。