-1

I'm trying to give myself Excercises using imports and keeping programs and modules separate from each other. In this particular excercise I've given myself, I have a basic feet-to-inches and inches-to-feet converter.

Everything works except for one problem. Where the answer is displayed on the GUI, a new answer covers the old answer, rather than replace.

import tkinter as tk
from tkinter import E, W
import Converter

window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)

def feet_to_inches_function(old_label):
    old_label.grid_forget()
    answer_label = tk.Label(window, text=Converter.feet_to_inches(int(entry_bar.get())))
    answer_label.grid(row=5)

def inches_to_feet_function(old_label):
    old_label.grid_forget()
    answer = tk.Label(window, text=Converter.inches_to_feet(int(entry_bar.get())))
    answer.grid(row=5)

title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=lambda: feet_to_inches_function(answer_label))
itf_button = tk.Button(window, text="Inches to Feet", command=lambda: inches_to_feet_function(answer_label))
quit_button = tk.Button(window, text="Quit", command=window.destroy)

title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))

window.mainloop()

Also as a separate question,I'm having a hard time understanding mainloop.I know that it makes the program go in an infinate loop, but from where? I don't see any logical evidence that shows that my code is on an endless loop.

Thanks so much

Maz
  • 35
  • 8

1 Answers1

-1

Here's a solution for you:

import tkinter as tk
from tkinter import E, W


window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)


def feet_to_inches_function():
    answer_label.configure(text='As you want (INCHES)')


def inches_to_feet_function():
    answer_label.configure(text='As you want (FEET)')


title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=feet_to_inches_function)
itf_button = tk.Button(window, text="Inches to Feet", command=inches_to_feet_function)
quit_button = tk.Button(window, text="Quit", command=window.destroy)

title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))

window.mainloop()

All you need is to configure the text of the Label in both functions.

DaniyalAhmadSE
  • 807
  • 11
  • 20