我在这里没有几行代码可以在tkinter canvas
中绘制圆圈,但是我想做的就是将草图停在开始的位置,所以我要睡觉,但是我要在草图停到的位置停下来然后继续。
所以我写了这个函数
def stop_extent():
canvas.create_arc(0, 0, 0, 0, extent=0, outline="red", tags=("arc",))
extent = float(canvas.itemcget("arc", "extent"))
extent = extent + 5.0
canvas.itemconfigure("arc", extent=extent)
canvas.after(100, stop_extent)
并将所有值设置为0
,但仍然不会停止它,标签也没有配置。
import tkinter as tk
import time
def change_extent():
l.config(text="sketch in progress...")
canvas.create_arc(350, 100, 220, 220, extent=0, outline="yellow", tags=("arc",))
extent = float(canvas.itemcget("arc", "extent"))
extent = extent + 5.0
canvas.itemconfigure("arc", extent=extent)
canvas.after(100, change_extent)
def stop_extent():
l.config(text="sketch stopped...")
time.sleep(10)
l.config(text="sketch stopped...")
root = tk.Tk()
root.geometry("600x400")
canvas = tk.Canvas(root, width=200, height=200, background="dodger blue")
canvas.pack(fill="both", expand=True)
l = tk.Label(canvas, bg="dodger blue", fg="white")
l.place(x=250, y=20)
b= tk.Button(canvas, text="Start sketch", command=change_extent, width=25, fg='dodger blue')
b.place(x=220, y=330)
b= tk.Button(canvas, text="stop sketch", command=stop_extent, width=25, fg='dodger blue')
b.place(x=220, y=370)
root.mainloop()
答案 0 :(得分:0)
您可以在canvas.after
方法中跟踪change_extent
产生的回叫,并在canvas.after_cancel
方法中使用stop_extent
取消回叫;在下面的示例中,我使用全局变量stop
进行演示。
在GUI应用程序中使用time.sleep
通常是一个坏主意。
每次修改圆弧的范围时,您也不需要重新创建圆弧,但这超出了问题的范围。
import tkinter as tk
import time
def change_extent():
global stop
l.config(text="sketch in progress...")
extent = float(canvas.itemcget(arc_item, "extent"))
canvas.itemconfigure("arc", extent=extent+5)
stop = canvas.after(100, change_extent)
def stop_extent():
l.config(text="sketch stopped...")
print(stop)
canvas.after_cancel(stop)
canvas.itemconfigure("arc", extent=0) # <- reset to straight line start
stop = None
root = tk.Tk()
root.geometry("600x400")
canvas = tk.Canvas(root, width=200, height=200, background="dodger blue")
canvas.pack(fill="both", expand=True)
arc_item = canvas.create_arc(350, 100, 220, 220, extent=0, outline="yellow", tags=("arc",))
l = tk.Label(canvas, bg="dodger blue", fg="white")
l.place(x=250, y=20)
b= tk.Button(canvas, text="Start sketch", command=change_extent, width=25, fg='dodger blue')
b.place(x=220, y=330)
b= tk.Button(canvas, text="stop sketch", command=stop_extent, width=25, fg='dodger blue')
b.place(x=220, y=370)
root.mainloop()