Python Forum

Full Version: Help, a script line is skipped
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi, sorry for my bad English,
I tried this code of my, it works but a line is skipped

from tkinter import *
from tkinter.ttk import *

tk=Tk()
progress=Progressbar(tk,orient=HORIZONTAL,length=500,mode='determinate')

def bar():
    progress['value']=50   # <= this line is skipped
    if not 'MT' in globals():
        global MT
        try:
            import dl_translate as dlt
        except:
            os.system("pip install dl_translate")
        MT = dlt.TranslationModel()
    progress['value']=100
    
progress.pack()
Button(tk,text='Test Progress Bar',command=bar).pack()
mainloop()
literally, I don't know what really happen,
I hope some advanced user can help me with this case,
thank you for reading, have a nice day
The line is not skipped. You set progress to 50, then you later set progress to 100. Between setting progress to 50 and 100 you block mainloop from running so the display cannot be updated, which is one of the things mainloop does.

This example toggles between calling .update() for the progress bar and having mainloop() update the display. Push the button the first time and the bar appears to jump directly to 200. Press the button again and the bar appears to ramp to 200. Notice in both cases that the button remains pressed until the bar reaches 200.
import tkinter as tk
import tkinter.ttk as ttk
import time

root = tk.Tk()
progress = ttk.Progressbar(root, orient=tk.HORIZONTAL, length=200, mode='determinate')
update = True

def bar():
    global update
    progress['value'] = 0
    progress.update()
    update = not update
    for i in range(0, 200, 10):
        progress['value'] = i
        if update:
            progress.update()
        time.sleep(0.1)

progress.pack()
tk.Button(root, text='Test Progress Bar', command=bar).pack()
root.mainloop()
I seriously doubt that the line was skipped
add following line after line 8, then rerun
print(f"progress['value']: {progress['value']}")
thank you for the replies,

@deanhystad
so what the solution?

@Larz60+
[attachment=1502]
@deanhystad
thank you for the solution
I will give you a reputation point
The solution is you should not write button callbacks that take a long time to complete. If you are doing something that takes significant time, see if you can break it into smaller processing pieces and do a little at a time.
import tkinter as tk
import tkinter.ttk as ttk
import time

def start_bar():
    progress.set(0)
    update_bar()

def update_bar():
    if (value := progress.get()) < 200:
        # Do some small part of the work
        progress.set(value + 10)
        root.after(100, update_bar)

root = tk.Tk()
progress = tk.IntVar(value=0)
ttk.Progressbar(root, orient=tk.HORIZONTAL, length=200, mode='determinate', variable=progress).pack()
tk.Button(root, text='Test Progress Bar', command=start_bar).pack()
root.mainloop()
Another solution is to do the lengthy processing in a separate thread. Do you know how threads work in Python?