2013-04-15 56 views
0

我在写一个GUI,将摄氏温度转换为华氏温度,反之亦然。 我需要输入框的摄氏温度从0.0开始(这是)和华氏从32.0开始(我不知道该怎么做)。如何获得具有设定值的输入框? 这是我对程序的构造函数的代码:tkinter设置值在输入框中

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) 

回答

3

目前,您只是覆盖self.fahrenheitVar = 32.0当你以后做self.fahrenheitVar = DoubleVar()。您不妨删除__init__中的前两行。

你只需要设置在DoubleVar喜欢使用set方法的价值,

self.fahrenheitVar = DoubleVar() 
self.fahrenheitVar.set(32.0) 
+0

谢谢你,这是很容易做到。 – tinydancer9454