tkinter 在输入框中设置值 [英] tkinter set values in entry box

查看:91
本文介绍了tkinter 在输入框中设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

推荐答案

目前,当您稍后执行 self.fahrenheitVar = DoubleVar()<时,您只是覆盖了 self.fahrenheitVar = 32.0/代码>.您不妨删除 __init__ 中的前两行.

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__.

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

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

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

这篇关于tkinter 在输入框中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆