Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter Animation , Canvas shapes overlap

I am trying to make an animation block where the animation within a canvas block changes according to a real time condition. But as soon as the condition changes , my previous animation and new animation overlaps. Is there any way to avoid this.

from tkinter import *
import time
import psutil




root=Tk()
root.geometry('300x300')

r11="black"
r21="black"
r31="black"
r41="black"
r51="black"








c= Canvas(root, height=300,width=300,bg="black")

c.pack(side=BOTTOM)

c.create_rectangle(100, 100, 200, 150,outline="white", fill="black")
c.create_rectangle(200, 120, 205, 130,outline="white", fill="black")





c1=c.create_rectangle(102, 102, 118, 148,outline=r11, fill=r11)
c2=c.create_rectangle(122, 102, 138, 148,outline=r21, fill=r21)
c3=c.create_rectangle(142, 102, 158, 148,outline=r31, fill=r31)
c4=c.create_rectangle(162, 102, 178, 148,outline=r41, fill=r41)
c5=c.create_rectangle(182, 102, 198, 148,outline=r51, fill=r51)


while True:
            battery=psutil.sensors_battery()
            
            if battery.power_plugged:
                if battery.percent<16:
                    x="red"
                elif battery.percent>=16 and battery.percent<80:
                    x="yellow"
                else:
                    x="green"
                
                r11,r21,r31,r41,r51="black","black","black","black","black"
  


                c.itemconfig(c1,fill=r11)
                c.itemconfig(c2,fill=r21)
                c.itemconfig(c3,fill=r31)
                c.itemconfig(c4,fill=r41)
                c.itemconfig(c5,fill=r51)
                root.update()
                time.sleep(1)
                r11=x
                c.itemconfig(c1,fill=r11)
                c.itemconfig(c2,fill=r21)
                c.itemconfig(c3,fill=r31)
                c.itemconfig(c4,fill=r41)
                c.itemconfig(c5,fill=r51)
                root.update()
                time.sleep(1)
                r21=x
                c.itemconfig(c1,fill=r11)
                c.itemconfig(c2,fill=r21)
                c.itemconfig(c3,fill=r31)
                c.itemconfig(c4,fill=r41)
                c.itemconfig(c5,fill=r51)
                root.update()
                time.sleep(1)
                r31=x
                c.itemconfig(c1,fill=r11)
                c.itemconfig(c2,fill=r21)
                c.itemconfig(c3,fill=r31)
                c.itemconfig(c4,fill=r41)
                c.itemconfig(c5,fill=r51)
                root.update()
                time.sleep(1)
                r41=x
                c.itemconfig(c1,fill=r11)
                c.itemconfig(c2,fill=r21)
                c.itemconfig(c3,fill=r31)
                c.itemconfig(c4,fill=r41)
                c.itemconfig(c5,fill=r51)
                root.update()
                time.sleep(1)
                r51=x
                c.itemconfig(c1,fill=r11)
                c.itemconfig(c2,fill=r21)
                c.itemconfig(c3,fill=r31)
                c.itemconfig(c4,fill=r41)
                c.itemconfig(c5,fill=r51)
                root.update()
                time.sleep(1)
            else:
                if battery.percent<16:
                    r11="red"
                elif battery.percent>=16 and battery.percent<80:
                    r11="yellow"
                else:
                    r11="green"

                
                


                c.coords(c1,102,102,102+(battery.percent)-4,148)
                c.itemconfig(c1,fill=r11)
                root.update()
                time.sleep(1)

It also shows an error after i close the tkinter window. The error shows-

Traceback (most recent call last):
  File "c:\Users\yasht\Desktop\class assignment stt\t.py", line 116, in <module>
    c.coords(c1,102,102,102+(battery.percent)-4,148)
  File "C:\Users\yasht\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2766, in coords
    self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Resize the initial Canvas blocks for both the animations using coords function

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading