Python - Tkinter - Label Not Updating -


any ideas why leftresult_label label not update? function seems work label not update. have looked everywhere , can't find answer. 'left' value gets set label not change.

from tkinter import *  root = tk(classname="page calculator")  read = intvar()  total = intvar()  left = intvar()  read.set(1)  total.set(1)  left.set(1)  read_label = label(root,text="pages read:") read_label.grid(column=1, row=1)  total_label = label(root,text="total pages:") total_label.grid(column=1, row=2)  read_entry = entry(root,textvariable=read) read_entry.grid(column=2, row=1)  total_entry = entry(root,textvariable=total) total_entry.grid(column=2, row=2)  def func1():     left.set(total.get() - read.get())     print(left.get())  calculate_button = button(root,text="calculate",command= func1) calculate_button.grid(column=2, row=3)  percenet_label = label(root,text="percent finished:") percenet_label.grid(column=1, row=4)  left_label = label(root,text="pages left:") left_label.grid(column=1, row=5)  percenetresult_label = label(root,text=left.get()) percenetresult_label.grid(column=2, row=4)  leftresult_label = label(root,text="") leftresult_label.grid(column=2, row=5)  root.mainloop() 

to make function job, you'd rather have label:

leftresult_label = label(root, textvariable=left) 

once it's tkinter class variable, tkinter takes care when change value. once click button,

def func1():     left.set(total.get() - read.get())     percent.set(int(read.get()*100/total.get())) 

left , percent values, instances of tkinter.intvar() class have immidiate effect on widgets (labels in case) values set textvariable, have @ entry widgets.

here full code:

from tkinter import *  root = tk(classname="page calculator") read = intvar() total = intvar() left = intvar() percent = intvar()  read.set(1) total.set(1) left.set(1) percent.set(1)  def func1():     left.set(total.get() - read.get())     percent.set(int(read.get()*100/total.get()))  read_label = label(root,text="pages read:") read_label.grid(column=1, row=1)  read_entry = entry(root,textvariable=read) read_entry.grid(column=2, row=1)  total_label = label(root,text="total pages:") total_label.grid(column=1, row=2)  total_entry = entry(root,textvariable=total) total_entry.grid(column=2, row=2)   calculate_button = button(root,text="calculate",command= func1) calculate_button.grid(column=2, row=3)  percenet_label = label(root,text="percent finished:") percenet_label.grid(column=1, row=4)  left_label = label(root,text="pages left:") left_label.grid(column=1, row=5)  percenetresult_label = label(root,textvariable=percent) percenetresult_label.grid(column=2, row=4)  leftresult_label = label(root,textvariable=left) leftresult_label.grid(column=2, row=5)  root.mainloop() 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -