在python中更改默认的tkinter条目值

时间:2016-10-05 16:06:08

标签: python tkinter

checkLabel = ttk.Label(win,text = "  Check Amount  ", foreground = "blue")
checkLabel.grid(row = 0 , column = 1)
checkEntry = ttk.Entry(win, textvariable = checkVariable)
checkEntry.grid(row = 1, column = 1, sticky = 'w')

如何更改defualt输入字段显示0?

2 个答案:

答案 0 :(得分:1)

使用条目小部件的功能.insert().delete()。这是一个例子。

entry = tk.Entry(root)
entry.insert(END, "Hello") # this will start the entry with "Hello" in it
# you may want to add a delay or something here. 
entry.delete(0, END) # this will delete everything inside the entry
entry.insert(END, "WORLD") # and this will insert the word "WORLD" in the entry.

另一种方法是使用Tkinter StrVar。以下是使用str变量的示例。

entry_var = tk.StrVar()
entry_var.set("Hello")
entry = tk.Entry(root, textvariable=entry_var) # when packing the widget
# it should have the world "Hello" inside.
# here is your delay or you can make a function call.
entry_var.set("World") # this sets the entry widget text to "World"

答案 1 :(得分:0)

在创建checkVariable对象之前,将''的值设置为Entry(空字符串)?要使用的陈述是

checkVariable.set('')

但是checkVariable必须是StringVar,如果你想要整数值,你必须自己将输入值转换为整数。