TAGS :Viewed: 12 - Published at: a few seconds ago

[ 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():
    mileMent.set(0)      #Resets the Data

       #The GUI

myGui = Tk()

kiloMent = IntVar()
mileMent = IntVar()

myGui.title("Distance Converter")

myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')

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)


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")

        myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')

        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)


    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.mileMent.set(0)      #Resets the Data

if __name__ == '__main__':
    Gui = hisGui()