#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 # 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程序中,一切都在工作,但是无论我在条目中键入什么值,它总是传递一个0值,因此当我将输入转换为华氏度时,它将是32摄氏度,而摄氏度是-17.7778。我做错了什么?或者如何将视图中的条目值获取到我的控制器?谢谢您!在
这里有两个错误:
1-在
Counter.py
文件和Convert
类方法中,返回的不是正确的变量,而是return celsius
应该返回self.celsius
,并且self.fahrenheit
2-在
Controller.py
文件中:self.view.outputLabel["text"] = self.model.convertToFahrenheit(celsius)
这将不会更新label
,而应该执行以下操作:buttonPressed2
方法也是如此编辑-1:
最好在
Convert
类中更改方程,以返回正确的float
结果:self.celsius = float((fahrenheit - 32.0) * (0.56))
self.fahrenheit = float((celsius * 1.8) + 32.0)
编辑-2: 这就是您的
^{pr2}$buttonPressed1
类的buttonPressed1
方法应该是:对于
buttonPressed2
,则为:相关问题 更多 >
编程相关推荐