Python MVC风格的GUI温度转换器 模型 控制器 问题:
在我的温度转换器GUI程序中,一切都在工作,但是无论我在条目中键入什么值,它总是传递一个0值,因此当我将输入转换为华氏温度时,它将是32,摄氏温度为-17.7778。我做错了什么,或者如何将视图中的输入值获取到控制器?谢谢大家! 这里有两个错误: 1-在Python MVC风格的GUI温度转换器 模型 控制器 问题:,python,user-interface,python-3.x,model-view-controller,tkinter,Python,User Interface,Python 3.x,Model View Controller,Tkinter,在我的温度转换器GUI程序中,一切都在工作,但是无论我在条目中键入什么值,它总是传递一个0值,因此当我将输入转换为华氏温度时,它将是32,摄氏温度为-17.7778。我做错了什么,或者如何将视图中的输入值获取到控制器?谢谢大家! 这里有两个错误: 1-在Counter.py文件和Convert类方法中,您没有返回正确的变量,而不是返回摄氏度您应该返回self.cercific,而self.fahrenheit 2-在Controller.py文件中: self.view.outputLabel[
Counter.py
文件和Convert
类方法中,您没有返回正确的变量,而不是返回摄氏度
您应该返回self.cercific
,而self.fahrenheit
2-在Controller.py
文件中:
self.view.outputLabel[“text”]=self.model.convertToFahrenheit(摄氏度)
这不会更新标签
,相反,您应该执行以下操作:
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()
按下按钮2的方法也是如此
编辑-1:
最好更改Convert
类中的方程式,以返回正确的float
结果:
self.centrics=float((华氏-32.0)*(0.56))
self.fahrenheit=float((摄氏度*1.8)+32.0)
编辑-2:
这是您的按钮按下1
转换类的方法应该是:
result = str(self.model.convertToFahrenheit(float(celsius))) #need to convert to string
self.view.outputLabel.config(text=result) #update the label with result
对于按钮,按2
如下:
def buttonPressed1(self):
celsius = self.view.entrySpace.get()
result = str(self.model.convertToFahrenheit(float(celsius)))
self.view.outputLabel.config(text=result)
您在self.view.mainloop()之后立即执行了root.destroy()
。你确定要这样做吗?是的,我非常肯定蒂格希·哈利勒,谢谢你的回答。。我试着做你正在做的事情,但它给了我这个错误:AttributeError:“Controller”对象没有属性“华氏度”。我很困惑,我们是否应该将入口空间中的值传递给convertToFahrenheit?对不起,我还是Python的初学者:(@jaephillseo,用我提供给你的更正更新你发布的代码,让我们看看有什么问题wrong@jaephillseo,查看编辑-1和编辑-2非常感谢你,哈利勒。我不知道我能感谢你多少我希望有一天我能和你一样好!很高兴我能帮上忙……)
result = str(self.model.convertToFahrenheit(float(celsius))) #need to convert to string
self.view.outputLabel.config(text=result) #update the label with result
def buttonPressed1(self):
celsius = self.view.entrySpace.get()
result = str(self.model.convertToFahrenheit(float(celsius)))
self.view.outputLabel.config(text=result)
def buttonPressed2(self):
fahrenheit = self.view.entrySpace.get()
result = str(self.model.convertToCelsius(float(fahrenheit)))
self.view.outputLabel.config(text=result)