How to make Progressbar in Kinter?

Asked

Viewed 95 times

-2

Hello, I wanted to make a progression in Kinter that when it came to the end it would stop, the progress of the progressibar would be activated by a button, but when the button is activated, the progress of the progressibar does not happen. I’m using the following code:


janela = Tk()
janela.geometry('500x500')
def iniciar():   
    import tkinter as tk
    from tkinter import ttk

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

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

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

            self.var_aux = tk.IntVar()

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

            self.progress.place(x = 0, y = 50)
            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(10, self.read_bytes)

    app = SampleApp()
    app.geometry('500x100')
    app.mainloop()

lb = Button(janela, text = 'começar', command = iniciar)
lb.place(x = 10, y = 10)

janela.mainloop()


1 answer

0

In fact the class SampleApp already has all the logic to create the window and a progress bar.


You can choose to work only with this class, then remove the variable janela:

import tkinter as tk
from tkinter import ttk

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

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

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

        self.progress.place(x = 0, y = 50)
        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(10, self.read_bytes)

app = SampleApp()
app.geometry('500x100')
app.mainloop()

This will already create a window, with the play button that lets you start the progress bar.

Browser other questions tagged

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