#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"})
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
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()
一切都在我的温度转换器GUI程序中工作,但是无论我在Entry中键入什么值,它总是传递0值,因此当我将输入转换为华氏温度时,它将是32和-17.7778摄氏度。我做错了什么或如何在视图中获取Entry值到我的控制器?谢谢!
答案 0 :(得分:1)
你有两个错误:
1 - 在您的Counter.py
文件和Convert
类方法中,您没有返回正确的变量,而是return celsius
您应该返回self.celsius
并且同样如此self.fahrenheit
2 - 在Controller.py
档案中:
self.view.outputLabel["text"] = self.model.convertToFahrenheit(celsius)
这不会更新label
,而应该执行以下操作:
result = str(self.model.convertToFahrenheit(float(celsius))) #need to convert to string
self.view.outputLabel.config(text=result) #update the label with result
同样适用于buttonPressed2
方法
修改-1:强>
更好地更改Convert
课程中的等式以返回正确的float
结果:
self.celsius = float((fahrenheit - 32.0) * (0.56))
self.fahrenheit = float((celsius * 1.8) + 32.0)
修改-2:强>
这就是buttonPressed1
类的Convert
方法应该是:
def buttonPressed1(self):
celsius = self.view.entrySpace.get()
result = str(self.model.convertToFahrenheit(float(celsius)))
self.view.outputLabel.config(text=result)
buttonPressed2
为:
def buttonPressed2(self):
fahrenheit = self.view.entrySpace.get()
result = str(self.model.convertToCelsius(float(fahrenheit)))
self.view.outputLabel.config(text=result)