IN PYTHON THE PROBLEM I can not get my clear button to reset and clear the resul
ID: 3682444 • Letter: I
Question
IN PYTHON
THE PROBLEM
I can not get my clear button to reset and clear the result entry(can not use destroy) and the enter a temperature entry to reset and clear (can not use destroy)
PYTHON CODE
from tkinter import *
class Application(Frame): # inherit from Frame.
def __init__(self):
Frame.__init__(self) #call superclass constructor
self.master.title("Temperature Converter") #give frame a title
self.master.geometry("330x230")
self.grid()
self._temperature = StringVar()
self._tcEntry = Entry(self, width=15, textvariable = self._temperature)
self._tcEntry.grid(row=0, column=2, pady=10)
Label(self, text="Enter a temperature").grid(row=0, column=1, padx=3, pady=10)
self._button = Button(self, text = "Convert Value", command = self._show)
self._button.grid(row=3, column=1, padx=5, pady=5)
self._result = StringVar()
self._resultEntry = Entry(self, width=15, textvariable = self._result)
self._resultEntry.grid(row=7, column=2, pady=10)
Label(self, text="Result").grid(row=7, column=1, padx=5, pady=5)
self.var = IntVar()
self._radioButton_1 = Radiobutton(self, text = "Convert to Fahrenheit", variable = self.var, value = 1, command = self._compute)
self._radioButton_1.grid(row=1, column=2, sticky=W, padx=10, pady=10)
self._radioButton_2 = Radiobutton(self, text = "Convert to Celsius", variable = self.var, value = 2, command = self._compute)
self._radioButton_2.grid(row=2, column=2, sticky=W, padx=10, pady=10)
self._button = Button(self, text = "Clear", command=self.destroy)
self._button.grid(row=3, column=2, pady=10)
def _compute(self):
num = self._tcEntry.get()
if (len(num) != 0):
num = float(num)
val = float(self.var.get())
if (val == 1):
self.answer = round(num * 1.8 + 32,1)
else:
self.answer = round((num - 32) / 1.8,1)
def _show(self):
self._compute()
self._result.set(self.answer);
def main():
Application().mainloop()
main()
THE ASSIGNMENT
PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets.
Temperature Converter GUI
Enter a temperature (Entry box)
Convert to Fahrenheit (radio button)
Convert to Celsius (radio button)
CONVERT VALUE CLEAR (clears both entry boxes)
Result (Entry box)
1. The user will enter a temperature in the first entry box. They will then click either the Convert to Fahrenheit or the Convert to Celsius radio button (but not both). And when they click the Convert button the value entered in the first entry box will be converted to the proper temperature and put into the Result or second entry box.
2. use a class that inherits Frame to implement your program.
3. Your 2 entry boxes need to have labels explaining the purpose of the entry boxes.
4. Round the result to 1 decimal place.
5. In addition to the Convert Value button, you need to have a Clear button that clears both entry boxes.
Explanation / Answer
Instead of destroy you need to set text to empty, see the _clear function
from tkinter import *
class Application(Frame): # inherit from Frame.
def __init__(self):
Frame.__init__(self) #call superclass constructor
self.master.title("Temperature Converter") #give frame a title
self.master.geometry("330x230")
self.grid()
self._temperature = StringVar()
self._tcEntry = Entry(self, width=15, textvariable = self._temperature)
self._tcEntry.grid(row=0, column=2, pady=10)
Label(self, text="Enter a temperature").grid(row=0, column=1, padx=3, pady=10)
self._button = Button(self, text = "Convert Value", command = self._show)
self._button.grid(row=3, column=1, padx=5, pady=5)
self._result = StringVar()
self._resultEntry = Entry(self, width=15, textvariable = self._result)
self._resultEntry.grid(row=7, column=2, pady=10)
Label(self, text="Result").grid(row=7, column=1, padx=5, pady=5)
self.var = IntVar()
self._radioButton_1 = Radiobutton(self, text = "Convert to Fahrenheit", variable = self.var, value = 1, command = self._compute)
self._radioButton_1.grid(row=1, column=2, sticky=W, padx=10, pady=10)
self._radioButton_2 = Radiobutton(self, text = "Convert to Celsius", variable = self.var, value = 2, command = self._compute)
self._radioButton_2.grid(row=2, column=2, sticky=W, padx=10, pady=10)
self._button = Button(self, text = "Clear", command=self._clear)
self._button.grid(row=3, column=2, pady=10)
def _compute(self):
num = self._tcEntry.get()
if (len(num) != 0):
num = float(num)
val = float(self.var.get())
if (val == 1):
self.answer = round(num * 1.8 + 32,1)
else:
self.answer = round((num - 32) / 1.8,1)
def _show(self):
self._compute()
self._result.set(self.answer);
def _clear(self):
self._result.set("")
self._temperature.set("")
def main():
Application().mainloop()
main()