Setting a Default Value in an Entry box in Tkinter Setting a Default Value in an Entry box in Tkinter tkinter tkinter

Setting a Default Value in an Entry box in Tkinter


You can use the insert method of the entry widget to insert any value you want:

value = pd.value_counts(df['month'].values, sort=False)[1]e1.insert(0, value)

You can also associate an instance of StringVar or IntVar by using the textvariable attribute, though most of the time that just creates an extra object you have to keep track of. In both cases you need a function call to both set and get the value, so in this case the textvariable gives no advantage and requires one extra line of code to create the variable.

v = IntVar()e1 = Entry(master, text=v)v.set(value)


Since you were creating IntVar() in your example You can create the IntVar() and set it as the text value for your entry (So that it becomes the default value). Example -

from tkinter import *def show_entry_fields():   print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))master = Tk()Label(master, text="First Name").grid(row=0)Label(master, text="Last Name").grid(row=1)v = IntVar()e1 = Entry(master, text=v)e2 = Entry(master)v.set(100)e1.grid(row=0, column=1)e2.grid(row=1, column=1)Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)Button(master, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)mainloop( )

The above code sets default value 100 for entry e1 , similarly create a new IntVar() for e2 if required.


You can also look into StringVar , if strings are what you need.


I have used this way using insert method

e1.insert(0, "a default value for e1")e2.insert(0, "b default value for e2")