Python MVC风格的GUI温度控制

2024-10-04 05:28:05 发布

您现在位置:Python中文网/ 问答频道 /正文

#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"})

模型

^{pr2}$

控制器

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。我做错了什么?或者如何将视图中的条目值获取到我的控制器?谢谢您!在


Tags: textimportselfviewtkinterdefresultside
1条回答
网友
1楼 · 发布于 2024-10-04 05:28:05

这里有两个错误:

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类的buttonPressed1方法应该是:

^{pr2}$

对于buttonPressed2,则为:

def buttonPressed2(self):
        fahrenheit = self.view.entrySpace.get()
        result = str(self.model.convertToCelsius(float(fahrenheit)))
        self.view.outputLabel.config(text=result)

相关问题 更多 >