[ Why can't I get my labels to clear when the "Reset" button is pressed? ]
I am making a program that converts kilometers to miles and miles to kilometers. When the values are displayed I want there to be a reset button that resets the values and the number the users numbers.
I can get the input of the numbers to clear out but not the label (finalKilo, finalMile). Any suggestions?
Also, is there any other way of displaying the calculated value without using a Label?
from tkinter import *
def closeWin():
myGui.destroy() #Close Window Function
def kiloFunc():
myText = kiloMent.get() #Kilometers to Miles Fuction
convert = 0.62
miles = myText * convert
finalKilo = Label(text = miles,fg='red',justify='center').place(x=200,y=80)
def mileFunc():
myText2 = mileMent.get() #Miles to Kilometers Function
convertTwo = myText2 // 0.62
finalMile = Label(text = convertTwo, fg = 'red',justify='center').place(x=200,y=170)
def reset_values():
kiloMent.set(0)
mileMent.set(0) #Resets the Data
finalKilo.set("")
finalMile.set("")
#The GUI
myGui = Tk()
kiloMent = IntVar()
mileMent = IntVar()
myGui.title("Distance Converter")
myGui.geometry("450x200+500+200")
myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')
myLabel.pack()
kiloEntry = Entry(myGui, textvariable = kiloMent,justify='center').pack()
kilo2milesButton = Button(text = "Kilometers to Miles!", command = kiloFunc).pack()
mileEntry = Entry(myGui, textvariable = mileMent,justify='center').place(x=130,y=105)
miles2kiloButton = Button(text = "Miles to Kilometers!", command = mileFunc).place(x=150,y=135)
reset = Button(text = "Reset Values!", command = reset_values).place(x=10,y=165)
quit = Button(text="Quit", command = closeWin).place(x=385,y=165)
myGui.mainloop()
Answer 1
Instead of creating new labels on each conversion, rather create a single label for the result and, instead of text
, assign a textvariable
to it which is an instance of StringVar
. On that variable, you can call set
to change the text of the label associated with the variable,
Answer 2
Callidior was correct, there were multiple mistakes :)
I've adjusted the code, put it in a class and now it should work.
Now it refers to the actual variable (miles and convertTwo) when it tries to change the labels. You referred to the labels themselves. In that case you probably should have done something like:
finalKilo.config(text = "")
finalMile.config(text = "")
Another problem was that you defined the label only after the an entry was given, while in the reset you reset both. So in case only one entry is given for the first time and the reset is pushed, an error pops up saying that one of the labels is not defined/None.
The code below should work.
from Tkinter import *
class hisGui:
def __init__(self):
self.myGui = Tk()
self.kiloMent = IntVar()
self.mileMent = IntVar()
self.myGui.title("Distance Converter")
self.myGui.geometry("450x200+500+200")
myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')
myLabel.pack()
self.miles = StringVar()
self.convertTwo = StringVar()
self.finalKilo = Label(textvariable = self.miles,fg='red',justify='center').place(x=200,y=80)
self.finalMile = Label(textvariable = self.convertTwo, fg = 'red',justify='center').place(x=200,y=170)
kiloEntry = Entry(self.myGui, textvariable = self.kiloMent,justify='center').pack()
kilo2milesButton = Button(text = "Kilometers to Miles!", command = self.kiloFunc).pack()
mileEntry = Entry(self.myGui, textvariable = self.mileMent,justify='center').place(x=130,y=105)
miles2kiloButton = Button(text = "Miles to Kilometers!", command = self.mileFunc).place(x=150,y=135)
reset = Button(text = "Reset Values!", command = self.reset_values).place(x=10,y=165)
quit = Button(text="Quit", command = self.closeWin).place(x=385,y=165)
self.myGui.mainloop()
def closeWin(self):
self.myGui.destroy() #Close Window Function
def kiloFunc(self):
myText = self.kiloMent.get() #Kilometers to Miles Function
convert = 0.62
self.miles.set(myText * convert)
def mileFunc(self):
myText2 = self.mileMent.get() #Miles to Kilometers Function
self.convertTwo.set(myText2 // 0.62)
def reset_values(self):
self.kiloMent.set(0)
self.mileMent.set(0) #Resets the Data
self.miles.set("")
self.convertTwo.set("")
if __name__ == '__main__':
Gui = hisGui()