Python Forum
Help, a script line is skipped
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help, a script line is skipped
#1
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
Reply
#2
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()
kucingkembar and BashBedlam like this post
Reply
#3
I seriously doubt that the line was skipped
add following line after line 8, then rerun
print(f"progress['value']: {progress['value']}")
kucingkembar likes this post
Reply
#4
thank you for the replies,

@deanhystad
so what the solution?

@Larz60+
   
Reply
#5
@deanhystad
thank you for the solution
I will give you a reputation point
Reply
#6
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?
kucingkembar likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to skip to a different line while importing a different script kat_gamer 2 2,251 Feb-03-2021, 04:10 AM
Last Post: deanhystad
  read the first line in my script chubbychub 1 1,768 Nov-09-2020, 03:18 AM
Last Post: Larz60+
  Script won't continue after command line skip671 7 4,880 Oct-15-2018, 07:37 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020