尝试每x秒发送一次消息,持续x个周期。

我有一个调用send_message的按钮,用户可以输入systemFrequency和systemRunTime。

Publish_message执行操作。

def publish_message(self):
...#do something

def send_message(self):
    systemFrequency = float(freqSettings.systemFrequency)
    systemRunTime = int(freqSettings.systemRunTime)
    global count

    count = 0

    if systemFrequency != 0:

        while (count < systemRunTime):
            self.schedule()

    else:
        self.publish_message()

def schedule(self):
    self.publish_message()
    self.increaseCount()

def increaseCount(self):
    global count
    count += 1


当前未在执行publish_message之前等待x秒钟。

最佳答案

好的,您可以使用tkinterafter方法。它计划在给定的毫秒数后调用一个函数。

def publish_message(self):
...#do something

def send_message(self):
    systemFrequency = float(freqSettings.systemFrequency)
    systemRunTime = int(freqSettings.systemRunTime)
    global count

    count = 0

    if systemFrequency != 0:
        self.schedule()
    else:
        self.publish_message()

def schedule(self):
    if count >= systemRunTime:
        return
    self.publish_message()
    self.increaseCount()
    # works if self.master is your window, change if it isn't
    self.master.after(1000, schedule, self)
    # after 1000 ms, call schedule function with parameter self

def increaseCount(self):
    global count
    count += 1

关于python - 如何在x倍的时间内每x秒发送send_message呢?与tkinter进口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56689814/

10-12 20:59