IN PYTHON THE PROBLEM I can\'t get my clear button to reset and clear the result
ID: 3682382 • Letter: I
Question
IN PYTHON
THE PROBLEM
I can't 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).
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.
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()
Explanation / Answer
#!/usr/bin/python3 from tkinter import * from tkinter import ttk root = Tk() usernameVal = StringVar() def submitForm(*args): try: print("submitForm pressed") print('name is %s' % usernameVal.get()) #mira como hace el print! #usernameVal.clear(1.0, END) ?? except ValueError: pass def main(): #container view mainframe = ttk.Frame(root, padding="3 3 12 12") # mainframe contained by root!, init mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) # add subview mainframe mainframe.columnconfigure(0, weight=1) mainframe.rowconfigure(0, weight=1) #UI widgets createLbl = ttk.Label(mainframe, text='Create login & Password') createLbl.grid(column=2 , row=1, sticky=(W,E)) # user name nameLbl = ttk.Label(mainframe, text='User Name') nameLbl.grid(column=1 , row=2, sticky=(W,E)) #usernameVal = StringVar() def submitForm(*args): try: print("submitForm pressed") print('name is %s' % usernameVal.get()) #mira como hace el print! #usernameVal.clear(1.0, END) ?? except ValueError: pass def main(): #container view mainframe = ttk.Frame(root, padding="3 3 12 12") # mainframe contained by root!, init mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) # add subview mainframe mainframe.columnconfigure(0, weight=1) mainframe.rowconfigure(0, weight=1) #UI widgets createLbl = ttk.Label(mainframe, text='Create login & Password') createLbl.grid(column=2 , row=1, sticky=(W,E)) # user name nameLbl = ttk.Label(mainframe, text='User Name') nameLbl.grid(column=1 , row=2, sticky=(W,E)) #usernameVal = StringVar() usernameTf = ttk.Entry(mainframe, textvariable = usernameVal) usernameTf.grid(column=2 , row=2, sticky=(W,E)) button = ttk.Button(mainframe, text='Create User', command=submitForm) button.grid(column=3 , row=2, sticky=(W,E)) #my loop root.mainloop() if __name__ == "__main__": main()