Updating a label from an entry field on button push with tkinter in Python 3.5.2 -
i trying create window line label, entry field, current value label, , "update value" button.
here example:
this have far. can entered value print console, can't seem work out how entered value , change currentvalue label reflect value pressing button:
from tkinter import* main=tk() #stringvar currentvalue in r0c2 currentvalue = stringvar(main, "0") #called setvalues button, looks content in entry box , updates "current" label def setvalues(): content = entry.get() print(content) #this kills program def exitprogram(): exit() #title , window size main.title("title") main.geometry("350x200") #descriptions on far left label(main, text="duration (min): ").grid(row=0, column=0) #entry boxes values amidship entry=entry(main, width=10) entry.grid(row=0, column=1) #displays value set to. currentvalue = label(textvariable=currentvalue) currentvalue.grid(row=0,column=2) #takes inputted values , sets them in "current" column using def setvalues setvalues=button(text='set values',width=30,command=setvalues) setvalues.grid(row=9, column=0, columnspan=2) #red button end program exitbutton=button(main, text='exit program',fg='white',bg='red',width=30, height=1,command=exitprogram) exitbutton.grid(row=20, column = 0, columnspan=2) main.mainloop()
there couple of problems code.
firstly, overwriting setvalues
function setvalues
button widget, , similarly, overwriting currentvalue
stringvar currentvalue
label.
to set stringvar, use .set
method.
don't use plain exit
in script, that's meant used in interactive interpreter session, proper exit function sys.exit
. however, in tkinter program can call .destroy
method of root window.
here's repaired version of code.
import tkinter tk main = tk.tk() #stringvar currentvalue in r0c2 currentvalue = tk.stringvar(main, "0") #called setvalues button, looks content in entry box , updates "current" label def setvalues(): content = entry.get() print(content) currentvalue.set(content) #this kills program def exitprogram(): main.destroy() #title , window size main.title("title") main.geometry("350x200") #descriptions on far left tk.label(main, text="duration (min): ").grid(row=0, column=0) #entry boxes values amidship entry = tk.entry(main, width=10) entry.grid(row=0, column=1) #displays value set to. currentvaluelabel = tk.label(textvariable=currentvalue) currentvaluelabel.grid(row=0,column=2) #takes inputted values , sets them in "current" column using def setvalues setvaluesbutton = tk.button(text='set values',width=30,command=setvalues) setvaluesbutton.grid(row=9, column=0, columnspan=2) #red button end program exitbutton = tk.button(main, text='exit program',fg='white',bg='red',width=30, height=1,command=exitprogram) exitbutton.grid(row=20, column = 0, columnspan=2) main.mainloop()
btw, it's idea avoid "star" imports. doing from tkinter import *
dumps 130 names namespace, unnecessary , creates possibility of name collisions, if star imports several modules. makes code less readable, since reader has remember names defined , ones came imported module(s).
Comments
Post a Comment