【问题标题】:pyqt start timer only oncepyqt 只启动一次计时器
【发布时间】:2020-06-09 16:31:52
【问题描述】:

如果时间到了,是否可以只启动一次Qtimer

self.timer = QtCore.QTimer()
self.timer.setInterval(10000)
self.timer.start(800)  # start only once, not every 800 milliseconds
self.timer.timeout.connect(self.function)

【问题讨论】:

    标签: python timer pyqt pyqt5


    【解决方案1】:

    为此,您必须启用singleShot 属性:

    self.timer = QtCore.QTimer()
    self.timer.setSingleShot(True)
    self.timer.setInterval(800)
    
    self.timer.timeout.connect(self.function)
    
    self.timer.start()
    

    或者

    QTimer.singleShot(800, self.function)
    

    【讨论】:

      【解决方案2】:

      除了 eyllanesc 提到的 signleShot 方法之外, 您可以在 self.function 中停止计时器。

      def function(self):
          self.timer.stop()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 2020-10-08
        • 2011-12-20
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多