在PySide6中使用QTimer很简单,您可以按照以下步骤进行操作:
- 导入
QtCore
模块,因为QTimer属于Qt的核心模块。
from PySide6.QtCore import QTimer
- 创建一个新的QTimer对象。
timer = QTimer()
- 通过设置timeout信号连接槽函数来指定所需的功能代码。timeout信号会以毫秒为单位周期性发出,也就是说每次周期完成后都会触发它。
timer.timeout.connect(your_function_name)
- 如果需要启动和停止计时器,可以使用
start()
和stop()
方法分别进行操作。
timer.start() # 开始计时器
timer.stop() # 停止计时器
现在您已经知道了如何在PySide6中使用QTimer了! 以下是完整的示例代码:
from PySide6.QtCore import QTimer
def my_function():
print("Hello, World!")
# create a QTimer object
timer = QTimer()
# connect timeout signal to the function
timer.timeout.connect(my_function)
# start the timer with an interval of 1000 milliseconds
timer.start(1000)
运行此代码后,"Hello, World!"将每秒打印一次,直到计时器被停止。