【问题标题】:tkinter set values in entry boxtkinter 在输入框中设置值
【发布时间】:2013-04-15 18:41:37
【问题描述】:

我正在编写一个将摄氏度转换为华氏度的 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)

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    目前,您只是在稍后执行 self.fahrenheitVar = DoubleVar() 时覆盖 self.fahrenheitVar = 32.0。你不妨把__init__中的前两行去掉。

    您只需要在DoubleVar 上使用set 方法设置值即可,

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

    【讨论】:

    • 谢谢,这很容易。
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多