Home > Blockchain >  Trying to display progress bar in Tkinter with Canvas
Trying to display progress bar in Tkinter with Canvas

Time:11-19

I was trying to add some progress bar to show some values independently like this:

VALUES OF PARAMETERS

So I use this code adding to my main program but when I code this:

class Processing1ra(tk.Frame):

  def __init__(self, parent):
    scalevar=IntVar()
    self.canvas = Canvas(
            bg = "#FFFFFF",
            height = 888,
            width = 1458,
            bd = 0,
            highlightthickness = 0,
            relief = "ridge"
        )
    self.scale=Scale(from_=0,to=100,orient="horizontal",variable=scalevar,command=self.volumelevel)
            self.scale.pack()
  def volumelevel(self,scalevar):
            a=16
            scalevar=int(scalevar)
            self.canvas.delete(self.rectangle_1)
            if scalevar <= 50:
                self.rectangle_1=self.canvas.create_rectangle(250.0,356.0,scalevar,a 250,fill="#AFDE14",outline="")
            else:
                self.rectangle_1=self.canvas.create_rectangle(250, 356,300, a 356,fill="#AFDE14",outline="")
                self.rectangle_2=self.canvas.create_rectangle(300, 356, scalevar,a 356, fill="#EB0701",outline="")

The problem is with self.canvas.delete(self.rectangle_1), I get those errors

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "d:\11.17.21\Interfaz4.py", line 544, in volumelevel
    self.canvas.delete(self.rectangle_1)
AttributeError: 'Processing1ra' object has no attribute 'rectangle_1'

But in the case to use self.canvas.delete(ALL) instead of self.canvas.delete(self.rectangle_1), other components will be erased

CodePudding user response:

You need to initialize self.rectangle_1 prior to using it. This can be done by assigning it a None value, and protecting the calls with a test on its value. (although tkinter will probably accept None without complaining)

like this:

class Processing1ra(tk.Frame):

    def __init__(self, parent):
        scalevar=IntVar()
        self.canvas = Canvas(
            bg = "#FFFFFF",
            height = 888,
            width = 1458,
            bd = 0,
            highlightthickness = 0,
            relief = "ridge"
        )
        self.scale = Scale(
            from_=0, 
            to=100, 
            orient="horizontal", 
            variable=scalevar, 
            command=self.volumelevel
        )
        self.scale.pack()
        self.rectangle_1 = None       # create the attribute here.
    
    
    def volumelevel(self, scalevar):
        a = 16
        scalevar = int(scalevar)
        
        if self.rectangle_1 is not None:
            self.canvas.delete(self.rectangle_1)
        
        if scalevar <= 50:
            self.rectangle_1 = self.canvas.create_rectangle(
                250.0, 
                356.0, 
                scalevar, 
                a 250, 
                fill="#AFDE14", 
                outline=""
            )
        else:
            self.rectangle_1 = self.canvas.create_rectangle(
                250,
                356,
                300, 
                a 356,
                fill="#AFDE14",
                outline=""
            )
            self.rectangle_2 = self.canvas.create_rectangle(
                300, 
                356, 
                scalevar,
                a 356, 
                fill="#EB0701",
                outline=""
            )
  • Related