0

我正在编写一个将摄氏度转换为华氏度的 GUI,反之亦然。我需要摄氏度的输入框从 0.0 开始(它确实如此),华氏度从 32.0 开始(我不知道该怎么做)。如何有一个设置值的输入框?这是我用于程序构造函数的代码:

class TempFrame(Frame):
    """FUI for the program to convert between Celsius and Fahrenheit"""
    def __init__(self):
        """Sets up the window and widgets"""
        self.celciusVar= 0.0
        self.fahrenheitVar= 32.0
        Frame.__init__(self)
        self.master.title("Temperature Conversion")
        self.grid()

        celsiusLabel = Label(self, text= "Celsius")
        celsiusLabel.grid(row = 0, column = 0)
        self.celsiusVar= DoubleVar()
        celsiusEntry = Entry(self, textvariable = self.celsiusVar)
        celsiusEntry.grid(row = 1, column = 0)

        fahrenheitLabel = Label(self, text= "Fahrenheit")
        fahrenheitLabel.grid(row = 0, column = 1)
        self.fahrenheitVar= DoubleVar()
        fahrenheitEntry = Entry(self, textvariable= self.fahrenheitVar)
        fahrenheitEntry.grid(row = 1, column = 1)

        button_1 = Button(self, text= ">>>>", command= self.celToFahr)
        button_1.grid(row = 2, column = 0)

        button_2 = Button(self, text= "<<<<", command=self.fahrToCel)
        button_2.grid(row = 2, column = 1)
4

1 回答 1

3

目前,您只是self.fahrenheitVar = 32.0在稍后执行时覆盖self.fahrenheitVar = DoubleVar()。您不妨删除__init__.

您只需要使用类似的set方法设置值,DoubleVar

self.fahrenheitVar = DoubleVar()
self.fahrenheitVar.set(32.0)
于 2013-04-15T19:03:00.213 回答