-1

I Am making a unit converter, in which the user can freely choose the units he wants and convert them I was making the converter for temperature units(Celsius, Kelvin and Fahrenheit), and i stumbled across an error in which the labels are getting overwritten even tho im trying to delete them

heres the function which deals with this:

# Phase 2
def phase_2():

    # The Subphases
    # Temp
    def temprature():

        mega.forget()

        heading = Label(root, text="Temprature", padx=40, pady=15, relief=SUNKEN)
        heading.pack()

        frameps = LabelFrame(root)
        frameps.pack(padx=10, pady=10)

        def conversion():

            # Answer Box
            answer = LabelFrame(frameps, padx=10)
            answer.grid(row=3, column=4, columnspan=1, padx=20, pady=20)

            empty = Label(answer, text="Value")
            empty.grid(row=0, column=0)

            # C functions
            def C_F():                 
                empty = Label(answer, text=f"{(int(val.get()) * (9 / 5)) + 32}°F")
                empty.grid(row=0, column=0)

            def C_K():
                empty = Label(answer, text=f"{int(val.get()) + 273.15}°K")
                empty.grid(row=0, column=0)

            def C_C():
                empty = Label(answer, text=f"{int(val.get())}°C")
                empty.grid(row=0, column=0)

            # F functions
            def F_C():
                empty = Label(answer, text=f"{(int(val.get()) - 32) * 5/9}°C")
                empty.grid(row=0, column=0)

            def F_K():
                empty = Label(answer, text=f"{(int(val.get()) - 32) * 5/9 + 273.15}°K")
                empty.grid(row=0, column=0)

            def F_F():
                empty = Label(answer, text=f"{int(val.get())}°F")
                empty.grid(row=0, column=0)

            # K functions
            def K_F():
                empty = Label(answer, text=f"{(int(val.get()) - 273.15) * 9/5 + 32}°F")
                empty.grid(row=0, column=0)

            def K_C():
                empty = Label(answer, text=f"{int(val.get()) - 273.15}°C")
                empty.grid(row=0, column=0)

            def K_K():
                empty = Label(answer, text=f"{int(val.get())}°K")
                empty.grid(row=0, column=0)


            # C probs
            if unit.get() == "C" and u.get() == "F":
                answer.forget()
                C_F()
            if unit.get() == "C" and u.get() == "K":
                answer.forget()
                C_K()
            if unit.get() == "C" and u.get() == "C":
                answer.forget()
                C_C()
            
            # F probs
            if unit.get() == "F" and u.get() == "C":
                answer.forget()
                F_C()
            if unit.get() == "F" and u.get() == "K":
                answer.forget()
                F_K()
            if unit.get() == "F" and u.get() == "F":
                answer.forget()
                F_F()
            
            # K probs
            if unit.get() == "K" and u.get() == "F":
                answer.forget()
                K_F()
            if unit.get() == "K" and u.get() == "C":
                answer.forget()
                K_C()
            if unit.get() == "K" and u.get() == "K":
                answer.forget()
                K_K()

as you can see, i keep trying 'answer.forget' so that the label is forgotten, and then rewritten every time that i use the converter button, but instead, this happens: The labels are getting overlapped even tho im trying to delete them

  • 1
    You can change the text of a `Label` instead of recreating it every time https://stackoverflow.com/questions/17125842/changing-the-text-on-a-label – Pietro Mar 29 '21 at 08:25
  • @Pietro could u give an example with reference to my code? – blankRiot96 Mar 29 '21 at 08:28

1 Answers1

1

The general idea is to build the layout once and change the values as needed, something like this:

import tkinter as tk


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.count = 0

        self.label = tk.Label(self, text="Click the button!")
        self.label.pack()

        self.some_button = tk.Button(self, text="Click", command=self.some)
        self.some_button.pack()

        self.close_button = tk.Button(self, text="Close", command=self.quit)
        self.close_button.pack()

    def some(self):
        self.count += 1
        self.label["text"] = f"You clicked {self.count} times."


if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

So when you click the Convert button, you should call a function in your app, and inside that you read the values and setting that you need, and update the text inside the labels with the new results.

This answer explains how to structure your app with an OOP approach, that I think is very useful as your app grows.

Cheers!

Pietro
  • 915
  • 2
  • 8
  • 14