Tkinter after()在Windows上变慢

时间:2016-11-14 06:08:58

标签: windows multithreading python-3.x tkinter

我正在开发一个程序,该程序使用firmata向Arduino发送具有特定比率/点数的点列表。为了测试它,我采用了一个程序并使用tk.after()函数对其进行了修改。如果需要,我还有spinbox来更改费率。

该程序在Windows中运行正常,默认速率为1毫秒。但是,当我将速率更改为2毫秒或其他时,它只能在打开谷歌浏览器时正确运行。否则,它会变得非常缓慢。 非常有趣的事情是,当我在Macbook上运行相同的代码时,它与该速率的任何值都运行良好。

import time
from tkinter import *
from tkinter import ttk
from timeit import default_timer as timer
root = Tk()
boxRate = IntVar()
isLooping = False

w = Label(root, text="Test")
w.pack()

frame = Frame(root, width=300, height=300)
frame.pack()

n = 0


def my_func():
    global end
    global start
    global isLooping
    global n
    if n >= 359:
        n = 0
        end = timer()
        print ("Total time: ", end - start)
        start = timer()
    else:
        n += 1

    #should be sending serial communication here

    if isLooping:
        root.after(boxRate.get(), my_func)


def run():
    global start
    global isLooping
    start = timer()
    isLooping = True
    root.after(boxRate.get(), my_func)

def exit():
    root.destroy()

def update():
    print ("Update")

def print_rate(rate):
    print (rate)

def stop():
    global isLooping
    isLooping = False

frequencyBox = Spinbox(root, from_ = 1, to = 100, textvariable = boxRate)
frequencyBox.pack()
frequencyBox.bind("<Return>", lambda event:print_rate(boxRate.get()))

runButton = ttk.Button(root, text = "Run", command = run)
runButton.pack()

updateButton = ttk.Button(root, text = "Update", command = lambda:print_rate(boxRate.get()))
updateButton.pack()

stopButton = ttk.Button(root, text = "Stop", command = stop)
stopButton.pack()

exitButton = ttk.Button(root, text = "Exit", command = exit)
exitButton.pack()

root.mainloop()

0 个答案:

没有答案