I need to add a class that inherits from the parent but, I am stuck please help.
ID: 3680570 • Letter: I
Question
I need to add a class that inherits from the parent but, I am stuck please help. The code I have so far is listed below, could you please show me the errors in this 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 (text box)
Convert to Fahrenheit (radio button)
Convert to Celsius (radio button)
CONVERT VALUE CLEAR (clears both entry boxes)
Result (text box)
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.
use a class that inherits Frame to implement your program.
Your 2 entry boxes need to have labels explaining the purpose of the entry boxes.
Round the result to 1 decimal place.
In addition to the Convert Value button, you need to have a Clear button that clears both entry boxes.
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._convert)
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._radioButton = Radiobutton(self, text = "Convert to Fahrenheit", variable = self._choice, value = 1, command = self._convert)
self._radioButton.grid(row=1, column=2, sticky=W, padx=10, pady=10)
self._radioButton2 = Radiobutton(self, text = "Convert to Celsius", variable = self._choice, value = 2, command = self._convert)
self._radioButton2.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 _choice():
self._choice = intVar()
self._choice.set
self._choice = fEntry(self, width=12, textvariable = self._box1var )
self._choice.grid(row=1, column=0, padx=5, pady=12)
self._choice.set
self._box2var = intVar()
self._choice = cEntry(self, width=12, textvariable = self._box1var)
self._choice.grid(row=2, column=0, padx=5, pady=12)
self._choice.set
'''
if self.choice == self._choice(f):
self._choice = int(round(tc - 32) / 1.8,1
self._choice.set()
elif choice == self._choice1(c):
self_choice = int(round(tc * 1.8 + 32),1
self._choice1.set()
else:
print ("Invalid entry")
'''
def _convert(self):
tc = float(self._temperature.get())
f = self._choice.set()
c = self._choice1.set()
def main():
Application().mainloop()
main()
Explanation / Answer
import tkinter
"""
Sets up a gui that accepts the degrees in fahrenheit and converts it to celsius
or accepts degree in celsius and converts it to fahrenheit when the appropriate
button is pressed.
"""
class TempConvert(tkinter.Frame):
def __init__(self):
tkinter.Frame.__init__(self)
self.pack()
self.createWidgets()
def createWidgets(self):
self.tempEntry_label = tkinter.Label(text = "Enter degrees in Fahrenheit: ")
self.tempEntry = tkinter.Entry()
self.tempEntry.delete(0, tkinter.END)
self.tempEntry.insert(0, "0")
self.tempEntry_label.pack(side = tkinter.LEFT)
self.tempEntry.pack(side = tkinter.LEFT)
self.celsButton = tkinter.Button(self)
self.celsButton["text"] = "Convert Fahrenheit to Celsius"
self.celsButton["command"] = self.convertocelsius
self.celsButton.pack(side = tkinter.LEFT)
self.displaycelsius = tkinter.Label(self)
self.displaycelsius.pack(side = tkinter.LEFT)
self.tempEntry2_label = tkinter.Label(text = "Enter degrees in Celsius: ")
self.tempEntry2 = tkinter.Entry()
self.tempEntry2.delete(0, tkinter.END)
self.tempEntry2.insert(0, "0")
self.tempEntry2_label.pack(side = tkinter.LEFT)
self.tempEntry2.pack(side = tkinter.LEFT)
self.fahrButton = tkinter.Button(self)
self.fahrButton["text"] = "Convert Celsius to Fahrenheit"
self.fahrButton["command"] = self.convertofahr
self.fahrButton.pack(side = tkinter.LEFT)
self.displayfahr = tkinter.Label(self)
self.displayfahr.pack(side = tkinter.LEFT)
self.quitButton = tkinter.Button(self)
self.quitButton["text"] = "Quit"
self.quitButton["command"] = self.quit
self.quitButton.pack(side = tkinter.LEFT)
def convertocelsius(self):
"""
Accepts degrees in fahrenheit and converts it to celsius.
"""
c = float(self.tempEntry.get())
self.displaycelsius["text"] = "Celsius = ", round(((5/9) * (c -32)), 2)
def convertofahr(self):
"""
Accepts degrees in celsius and converts it to fahrenheit.
"""
f = float(self.tempEntry2.get())
self.displayfahr["text"] = "Fahrenheit = ", round((((9/5)*f)+32), 2)
if __name__ == "__main__":
root = tkinter.Tk()
app = TempConvert()
app.mainloop()
root.destroy()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.