tkinter set values in entry box

2019-09-09 13:35发布

I'm writing a GUI that converts Celsius to Fahrenheit and vice versa. I need the entry box for Celsius to start at 0.0 (which it does) and Fahrenheit to start at 32.0 (which I don't know how to do). How do I have an entry box with a set value? This is the code I have for the constructor of the program:

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)

1条回答
霸刀☆藐视天下
2楼-- · 2019-09-09 13:58

Currently, you are just overriding self.fahrenheitVar = 32.0 when you later do self.fahrenheitVar = DoubleVar(). You might as well remove the first two lines in __init__.

You just need to set the value using set method on the DoubleVar like,

self.fahrenheitVar = DoubleVar()
self.fahrenheitVar.set(32.0)
查看更多
登录 后发表回答