tkinter after_cancel在python中 [英] Tkinter after_cancel in python
问题描述
随附的代码使用after方法在画布上随机绘制正方形。我希望在使用after_cancel方法单击鼠标按钮时停止绘图,bu继续绘制。
这是代码。
#! / usr / bin / env python
从Tkinter导入*
导入时间
导入随机
tk = Tk()
canvas = Canvas(tk,width = 1920,height = 1080,background =grey)
canvas.pack()
def xy(event):
xm,ym = event .x,event.y
def task():
w = random.randint(1,1000)
h = random.randint(1,1000)
canvas .create_rectangle(w,h,w + 150,h + 150)
def callback(event):
如果为True:
print(clicked2)
tk.after_cancel任务)
canvas.bind(< Button-1>,回调)
tk.after(1000,task)
tk.after(1000,task)
tk.mainloop()
你接近让它工作,你需要做的是将 tk.after(...)
设置为一个变量,而不是要停止的重复函数,那么当你想使用 after_cancel(...)
时,你调用这个变量,它会停止所有的方法名称
$ b c> from Tkinter import *
import time
import random
tk = Tk()
canvas = Canvas(tk,width = 1920,height = 1080,background =gray)
canvas.pack()
def xy(event):
xm,ym = event.x,event.y
def task():
w = random.randint(1,1000)
h = random.randint(1,1000)
canvas.create_rectangle(w,h,w + 150,h + 150)
def callback(event):
如果为True:
print(clicked2)
#'solve' 。 方法。
tk.after_cancel(solve)
canvas.bind(< Button-1>,callback)
solve = tk.after(1000,task)
#下面的tk.after设置为'solve'一个变量。
solve = tk.after(1000,task)
tk.mainloop()
b $ b
这将停止after方法,并让tk窗口运行。
另一个解决方案是使用 tk.destroy()
或 tk.quit()
或 sys.exit code>或类似的回调函数...这些将杀死tk窗口虽然。
我认为这回答了问题,如果不只是说,欢呼。
The attached code randomly draws squares on the canvas using the "after" method. I expected to stop the drawing when I clicked the mouse button using the "after_cancel" method, bu it continues to draw..
Here is the code.
#! /usr/bin/env python
from Tkinter import *
import time
import random
tk = Tk()
canvas = Canvas(tk, width=1920, height=1080, background="grey")
canvas.pack()
def xy(event):
xm, ym = event.x, event.y
def task():
w=random.randint(1,1000)
h=random.randint(1,1000)
canvas.create_rectangle(w,h,w+150,h+150)
def callback(event):
if True:
print("clicked2")
tk.after_cancel(task)
canvas.bind("<Button-1>",callback)
tk.after(1000,task)
tk.after(1000,task)
tk.mainloop()
Alright, so you were close to getting it to work, what you need to do is set tk.after(...)
to a variable, not the recurring function you want to stop, then when you want to use after_cancel(...)
, you call that variable and it will stop all after methods with that name
source: How do I stop tkinter after function?
This will do it:
from Tkinter import *
import time
import random
tk = Tk()
canvas = Canvas(tk, width=1920, height=1080, background="grey")
canvas.pack()
def xy(event):
xm, ym = event.x, event.y
def task():
w=random.randint(1,1000)
h=random.randint(1,1000)
canvas.create_rectangle(w,h,w+150,h+150)
def callback(event):
if True:
print("clicked2")
# 'solve' is used here to stop the after... methods.
tk.after_cancel(solve)
canvas.bind("<Button-1>",callback)
solve = tk.after(1000,task)
# above and below tk.after is set to 'solve' a variable.
solve = tk.after(1000,task)
tk.mainloop()
This will stop the after methods and leave a tk window running.
Another solution this would be to use tk.destroy()
, or tk.quit()
or sys.exit()
or similar in the callback function... These will kill the tk window though.
I think this answers the question, if not just say, cheers.
这篇关于tkinter after_cancel在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!