Unknownzdx Unknownzdx - 1 month ago 19
Python Question

I can not properly update my progressbar

I'm trying to update my progressbar when a button is clicked and the function should update when commanded(submit button clicked) but this does not happen properly

def progress(*args): #progressbar is set to be 50% completed
p.step(50)
def submitted(*args): #Progressbar is set to be fully completed and states information recorded
p.step(100)
messagebox.showinfo("Information Submitted", "Your information has been recorded.")

def clear(*args): #Clears everything and returns gui to start of program
l.selection_clear(0, END)
t.delete('1.0', END)
p.step(0)

#Sets title and creates gui
root=Tk()

#Configures column and row settings and sets padding
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

#Creates list of countries alphabetically
countries=["Finland", "France", "Greece", "Iceland", "Spain"]

l=Listbox(mainframe, height=5)
l.grid(column=0, row=0, sticky=(N,W,E,S))


#Adds items in list to listbox
for i in countries:
l.insert('end', i)


#Creates progessbar widget
p=ttk.Progressbar(mainframe, orient=VERTICAL, length=200, mode='determinate')
p.grid(column=2, row=0, sticky=(N,S))

#Activates progress function if something in listbox is selected
l.bind('<<ListboxSelect>>', progress)

#Submitting calls submitted function to set progressbar to 100 and statemessage box has been completed
subbttn= ttk.Button(mainframe, text="Submit", command=submitted)
subbttn.grid(column=1, row=1, sticky=(S, W, E))

clearbttn= ttk.Button(mainframe, text="Clear", command=clear)
clearbttn.grid(column=1, row=1, sticky=(N, W, E))
#Clears all inputs and returns program to how it was in the beginning

#Runs loop for gui
root.mainloop()


After something in the listbox is clicked, the progressbar updates to 50% and that's good. However after clicking the submit button there is no change. Also if I change it to p.set(99) the progressbar seems to decrease in progress. I want the progressbar to be completely filled after clicking the submit button. Also after clciking the clear button, I want the progress bar to be at 0 and there is no change to the progressbar.

Answer

p.step(100) adds 100 and gets "result modulo maximum"
so you have "0+100 modulo 100" which gives 0.

But you can use p["value"] = 100


Button Clear doesn't change progressbar because there is unknown variable t in function clear() so you get error message before it can execute p.step(0).

But p.step(0) doesn't set value to 0 - it adds 0 to current value.

Comments