Adler Müller
Adler Müller

Reputation: 248

tkinter progressbar indeterminate

The following code starts the progress bar by clicking start

# root window
root = tk.Tk()
root.geometry('300x120')
root.title('Progressbar Demo')

root.grid()

# progressbar
pb = ttk.Progressbar(
    root,
    orient='horizontal',
    mode='indeterminate',
    length=280
)
# place the progressbar
pb.grid(column=0, row=0, columnspan=2, padx=10, pady=20)



start_button = ttk.Button(
    root,
    text='Start',
    command=pb.start
    )
start_button.grid(column=0, row=1, padx=10, pady=10, sticky=tk.E)


root.mainloop()

how can the progressbar be started without a button? so just by calling a function? So basically I want the progressbar to start directly by executing the code. Something like this:

# root window
root = tk.Tk()
root.geometry('300x120')
root.title('Progressbar Demo')

root.grid()

# progressbar
pb = ttk.Progressbar(
    root,
    orient='horizontal',
    mode='indeterminate',
    length=280
)
# place the progressbar
pb.grid(column=0, row=0, columnspan=2, padx=10, pady=20)


pb.start

root.mainloop()

Upvotes: 1

Views: 609

Answers (1)

Sheik-Yabouti
Sheik-Yabouti

Reputation: 100

Firstly I've made a slight change to your code, calling in specifics imports from the Tkinter libraries. which can be seen below. This avoids using "from Tkinter import *" which can cause issues later down the line.

from tkinter import Tk 
from tkinter.ttk import Progressbar 

# root window 
root = Tk() 
root.geometry('300x120') 
root.title('Progressbar Demo')

root.grid()

# progressbar 
pb = Progressbar(
    root,
    orient='horizontal',
    mode='indeterminate',
    length=280 )

# place the progressbar 
pb.grid(column=0, row=0, columnspan=2, padx=10, pady=20)

This is the code that calls the progress bar to start on execution. You needed to add parentheses after calling the "pb.start". The code should work how you want now.

pb.start()

root.mainloop()

Upvotes: 2

Related Questions