1

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()
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Adler Müller
  • 248
  • 1
  • 14

1 Answers1

2

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()
Sheik-Yabouti
  • 85
  • 1
  • 9
  • Thanks for your answer. Unfortunately, another question arose for me. I added the following code to your example and by executing you see: pb.start() starts after print-commands. Do you now what is the reason for that? pb.start() import time print("Before the sleep statement") time.sleep(5) print("After the sleep statement") root.mainloop() – Adler Müller Sep 07 '21 at 08:40
  • I'll look into it for you, I'll hit you back when I've got something. – Sheik-Yabouti Sep 07 '21 at 09:14
  • okay thanks. I have created a new question for it: https://stackoverflow.com/questions/69085192/progressbar-starts-after-print-statements – Adler Müller Sep 07 '21 at 09:19