Hi, I’d like to do a Progressbar with Tkinter, can you help me?

Asked

Viewed 31 times

-4

I wanted that when I pressed the button it started and when I arrived at the end disappeared, but neither the bar nor the button appear in the window created, it does not trigger an error on the screen.?

Here’s the code I’m using:

from tkinter import ttk

class SampleApp(tk.Tk):
    def _init_(self,*args, **kwargs):

        tk.Tk._(self,*args, **kwargs)        
        self.button = ttk.Button(text = 'play', command = self.start)
        self.button.pack()
        self.progress = ttk.Progressbar(self, orient = "horizontal", length = 200, mode = "determinate")
        self.progress.place(x = 0, y = 10)
        self.bytes = 0
        self.maxbytes = 0

    def start(self):
        self.progress['values'] = 0
        self.maxbytes = 50000
        self.progress['maximun'] = 50000
        self.read_bytes()


    def read_bytes (self):
        self.bytes += 500
        self.progress['values'] = self.bytes

        if self.bytes < self.maxbytes:
            self.after(100, self.read_bytes)

app = SampleApp()
app.mainloop()


1 answer

1


The problem is you didn’t put underline double in the builder’s creation. What you did was put _init_ when should I put __init__, thus the program never entered the method.

Another mistake you made was calling the builder Tk. You used a single underline instead of calling the method __init__.

See below the corrected method:

class SampleApp(tk.Tk):
    def __init__(self,*args, **kwargs):

        tk.Tk.__init__(self,*args, **kwargs)

        self.button = ttk.Button(text = 'play', command = self.start)
        self.button.pack()

        self.progress = ttk.Progressbar(self, orient = "horizontal", length = 200, mode = "determinate")
        self.progress.place(x = 0, y = 10)

        self.bytes = 0
        self.maxbytes = 0

Now leaving the part of building your graphical interface, there is another problem in your code referring to ProgressBar.

You are trying to advance the progress bar through the attribute values that does not even exist. To use the progress bar correctly, you must create an object of IntVarand link it to your ProgressBar.

To set the slider advance, change the value of your slider object IntVar through the method set. That way, your program will work perfectly.

class SampleApp(tk.Tk):
    def __init__(self,*args, **kwargs):

        tk.Tk.__init__(self,*args, **kwargs)

        self.button = ttk.Button(text = 'play', command = self.start)
        self.button.pack()

        self.var_aux = tk.IntVar()

        self.progress = ttk.Progressbar(
            self, orient = "horizontal",
            length = 200, mode = "determinate",
            variable = self.var_aux
            )

        self.progress.place(x = 0, y = 10)

        self.bytes = 0
        self.maxbytes = 0

    def start(self):

        self.maxbytes = 50000
        self.progress['maximum'] = self.maxbytes
        self.read_bytes()


    def read_bytes(self):

        self.bytes += 500
        self.var_aux.set(self.bytes)

        if self.bytes < self.maxbytes:
            self.after(100, self.read_bytes)
  • Thanks! It worked.

  • and how do I stop when the progressibar arrives at the end the program execute an action?

  • Then you’ll have to create your own code to check the progress of the progressbar and do something.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.