Home > Enterprise >  Tkinter animation won't stop at certain point
Tkinter animation won't stop at certain point

Time:11-15

I have a problem with a school project that I am currently doing. I have to create and remove semicircles from circular clippings. And is there a possible solution that I part Tkinter window into three parts (header, body and footer)?

import tkinter
import threading
from tkinter import *
import tkinter as tk

root = tkinter.Tk()
root.geometry("800x800")
root.resizable(0, 0)

def menjajKanvas(c,arc):  # changeCanvas
    s=0
    while True:
        c.itemconfig(arc,extent=s,fill="red")
        s =1
        time.sleep(0.01)
c = tk.Canvas(root, height=250, width=300, bg="blue")
c.pack()
arc = c.create_arc(10,50,240,210, extent=150, outline="red", tags=("arc",))\
threading.Thread(target=menjajKanvas,args=(c,arc)).start()
root.mainloop()

CodePudding user response:

Tkinter doesn't support threading in the sense that only one thread—usually the main one—can access it in multi-threaded applications. A workaround for that limitation can often be implemented by using the widget Screenshot

CodePudding user response:

To achive what you want, you should use the after(ms,function,*arguments) method. You either can break the loop with after_cancel(alarm_id) or you stop calling the function within itself if, like in the sampel below.

In addition:

  1. import modules just once and stick to it
  2. Dont touch widgets outside of the thread of tkinters mainloop

import tkinter as tk

root = tk.Tk()
root.geometry("800x800")
root.resizable(0, 0)

s = 0
def menjajKanvas(arc):
    global s
    s =1
    c.itemconfig(arc,extent=s,fill="red")
    if s < 180:
        root.after(50,menjajKanvas,arc)
    
c = tk.Canvas(root, height=250, width=300, bg="blue")
c.pack()
arc = c.create_arc(10,50,240,210,
                   extent=150,
                   outline="red",
                   fill='red',
                   tags=("arc",))
menjajKanvas(arc)
root.mainloop()
  • Related