Python MVC style GUI temperature converter
#The view (GuiTest.py)
import tkinter
import Controller
class MyFrame(tkinter.Frame):
def __init__(self, controller):
tkinter.Frame.__init__(self)
self.pack()
self.controller = controller
#Output Label
self.outputLabel = tkinter.Label(self)
self.outputLabel["text"] = ("")
self.outputLabel.pack({"side":"right"})
#Entry Space
self.entrySpace = tkinter.Entry(self)
self.entrySpace["text"] = ("")
self.entrySpace.pack({"side":"left"})
#two convert buttons
self.convertButton=tkinter.Button(self)
self.convertButton["text"]= "Fahrenheit to Celsius"
self.convertButton["command"]=self.controller.buttonPressed2
self.convertButton.pack({"side":"left"})
self.convertButton2=tkinter.Button(self)
self.convertButton2["text"]= "Celsius to Fahrenheit"
self.convertButton2["command"]=self.controller.buttonPressed1
self.convertButton2.pack({"side":"left"})
#Quit button
self.quitButton = tkinter.Button(self)
self.quitButton["text"] = "Quit"
self.quitButton["command"] = self.quit
self.quitButton.pack({"side":"right"})
Model
import tkinter
import GuiTest
import Controller
class Convert:
def __init__(self):
self.fahrenheit = 0
self.celsius = 0
def convertToCelsius(self, fahrenheit):
self.celsius = float((self.fahrenheit - 32)) * (5/9)
return self.celsius
def convertToFahrenheit(self, celsius):
self.fahrenheit = float((self.celsius * (9/5))) + 32
return self.fahrenheit
Controller
import tkinter
import GuiTest # the VIEW
import Counter # the MODEL
class Controller:
def __init__(self):
"""
This starts the Tk framework up
"""
root = tkinter.Tk()
self.model = Counter.Convert()
self.view = GuiTest.MyFrame(self)
self.view.mainloop()
root.destroy()
def buttonPressed1(self):
result = str(self.model.convertToFahrenheit(self.celsius))
self.view.outputLabel.config(text = result)
def buttonPressed2(self):
result = str(self.model.convertToCelsius(self.fahrenheit))
self.view.outputLabel.config(text = result)
if __name__ == "__main__":
c = Controller()
Question
Everything works in my graphical temperature converter program, however no matter what value I enter into the record it always passes the value 0, so when I convert the input to measure to Fahrenheit it will be 32 and -17.7778 for the target. What am I doing wrong or how do I get the Entry value in the view for my controller? Thank!
source to share
You have two errors here:
1 - In your file Counter.py
and in the class methods, Convert
you are not returning the correct variables, instead return celsius
you must return self.celsius
, and also forself.fahrenheit
2 - In the file Controller.py
:
self.view.outputLabel["text"] = self.model.convertToFahrenheit(celsius)
This won't update label
, you should instead do something like:
result = str(self.model.convertToFahrenheit(float(celsius))) #need to convert to string
self.view.outputLabel.config(text=result) #update the label with result
The same applies to the method buttonPressed2
EDIT-1:
Better change your equations in the class Convert
to return the correct result float
:
self.celsius = float((fahrenheit - 32.0) * (0.56))
self.fahrenheit = float((celsius * 1.8) + 32.0)
EDIT-2:
This is your buttonPressed1
class method Convert
:
def buttonPressed1(self):
celsius = self.view.entrySpace.get()
result = str(self.model.convertToFahrenheit(float(celsius)))
self.view.outputLabel.config(text=result)
And for buttonPressed2
how:
def buttonPressed2(self):
fahrenheit = self.view.entrySpace.get()
result = str(self.model.convertToCelsius(float(fahrenheit)))
self.view.outputLabel.config(text=result)
source to share