问题描述
我在Tkinter的canvas.coords()
方法上遇到问题.我试图每1/60秒更新一次画布,并试图检测何时按下向右箭头键.问题是当我按住向右箭头一会儿时出现错误:
I am having problems with Tkinter's canvas.coords()
method. I am trying to update the canvas every 1/60 of a second and trying to detect when the Right arrow key is pressed. The problem is that when I keep the right arrow pressed for a while I get an error :
http://postimage.org/image/bdfk3m8gx/
我的脚本是'novo.py',代码如下.有什么帮助吗?我只是个初学者.
My script is 'novo.py' and the code is given below. Any help perhaps? I'm just a begginer.
import Tkinter, threading, time
def real_time(td, canvas):
while True:
time.sleep(1/60.0)
canvas.coords('line',1,1,200,200)
def func(event):
print 'key pressed'
root = Tkinter.Tk()
canvas = Tkinter.Canvas(root)
canvas.pack()
canvas.create_line(1,1,100,100, tag='line')
root.bind('<Right>', func)
thread = threading.Thread(target = real_time, args = (1/60.0, canvas))
thread.start()
root.mainloop()
推荐答案
Tkinter不是线程安全的,很多时候根本不需要线程.
Tkinter is not thread safe, and quite often threads simply aren't needed.
在您的情况下,您可以使用after
每N毫秒周期性地进行一次录音.例如,要每秒运行约60次,您将执行以下操作:
In your case you can use after
to periodically ron something every N milliseconds. For example, to run something approximately 60 times per second you would do something like this:
def real_time():
<do something here>
self.after(16, real_time)
然后,在程序启动时调用此函数一次:
Then, call this function once when your program starts up:
real_time()
这篇关于使用Tkinter绑定和画布进行线程化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!