PYTHON PYTHON PYTHON!!!!! The Gui has all the right buttons, it is correct, but
ID: 3682259 • Letter: P
Question
PYTHON PYTHON PYTHON!!!!! The Gui has all the right buttons, it is correct, but from there I am lost .Can someone please help. The code I have so far is listed below, could you please show me the errors in my code.
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 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.
My Code
from tkinter import *
root = Tk()
root.title("Temperature Converter")
mainframe = Frame(root)
root.geometry("330x230")
mainframe.grid()
def convert(event):
num = entry_1.get()
if (len(num) != 0):
num = float(num)
val = float(var.get())
elif (val == 1):
result = round(num * 1.8 + 32,1)
else:
result = round((num - 32) / 1.8,1)
button_1 = Button(root, text = "Convert Value",command=convert)
button_1.bind("<Button-1>", convert)
button_1.grid(row=5,column=1)
def clear(event):
entry_1 =Entry(root,resetToClear)
entry_2 =Entry(root,resetToClear,text = "Clear")
print("Change me later")
button_2 = Button(root, text = "Clear")
button_2.bind("<Button-1>", clear)
button_2.grid(row=5,column=2)
def result():
Entry_1=result(root,var = IntVar,command=result)
result.set(result);
label_1 = Label(root,text="Please enter a temperature")
label_2 = Label(root,text="Result")
entry_1 =Entry(root)
entry_2 =Entry(root)
label_1.grid(row=0,column=1,sticky = E)
label_2.grid(row=8,column=1,sticky = E)
entry_1.grid(row=0,column=2)
entry_2.grid(row=8,column=2)
var = IntVar(root)
radioButton_1 = Radiobutton(root, text = "Convert to Fahrenheit", variable = var, value = 1, command=result)
radioButton_1.grid(row=1, column=2, sticky=W, padx=10, pady=10)
radioButton_2 = Radiobutton(root, text = "Convert to Celsius", variable = var, value = 2, command=result)
radioButton_2.grid(row=2, column=2, sticky=W, padx=10, pady=10)
root.mainloop()
Explanation / Answer
from Tkinter import*
def convert():
celTemp = celTempVar.get()
fahTemp = fahTempVar.get()
if celTempVar.get() != 0.0:
celToFah = (celTemp * 9/5 + 32)
print celToFah
fahTempVar.set(celToFah)
elif fahTempVar.get() != 0.0:
fahToCel = ((fahTemp - 32) * (5.0/9.0))
print fahToCel
celTempVar.set(fahToCel)
def reset():
top = Toplevel(padx=50, pady=50)
top.grid()
message = Label(top, text = "Reset Complete")
button = Button(top, text="OK", command=top.destroy)
message.grid(row = 0, padx = 5, pady = 5)
button.grid(row = 1, ipadx = 10, ipady = 10, padx = 5, pady = 5)
fahTempVar.set(int(0))
celTempVar.set(int(0))
###MAIN###
root = Tk()
root.title("Temperature Converter")
mainframe = Frame(root)
mainframe.grid()
celTempVar = IntVar()
celTempVar.set(int(0))
fahTempVar = IntVar()
fahTempVar.set(int(0))
titleLabel = Label (mainframe, text = "Celcius/Fahrenheit Temperature", font = ("Arial", 20, "bold"), justify = CENTER)
titleLabel.grid(row = 1, column = 1, columnspan = 3, pady = 10, padx = 20)
celLabel = Label (mainframe, text = "Celcius: ", font = ("Arial", 16), fg = "red")
celLabel.grid(row = 2, column = 1, pady = 10, sticky = NW)
fahLabel = Label (mainframe, text = "Fahrenheit: ", font = ("Arial", 16), fg = "blue")
fahLabel.grid(row = 3, column = 1, pady = 10, sticky = NW)
celEntry = Entry (mainframe, width = 10, bd = 5, textvariable = celTempVar)
celEntry.grid(row = 2, column = 1, pady = 10, sticky = NW, padx = 125 )
fahEntry = Entry (mainframe, width = 10, bd = 5, textvariable = fahTempVar)
fahEntry.grid(row = 3, column = 1, pady = 10, sticky = NW, padx = 125 )
convertButton =Button (mainframe, text = "Convert", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = convert)
convertButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55)
resetButton = Button (mainframe, text = "Reset", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = reset)
resetButton.grid(row = 5, column = 1,ipady = 8, ipadx = 12, pady = 5, sticky = NW)
root.mainloop()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.