Setting a Default Value in an Entry box in Tkinter

18,473

Solution 1

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)

Solution 2

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.

Solution 3

I have used this way using insert method

e1.insert(0, "a default value for e1")
e2.insert(0, "b default value for e2")
Share:
18,473
jenryb
Author by

jenryb

Updated on June 07, 2022

Comments

  • jenryb
    jenryb almost 2 years

    I'm creating an user input box using Tkinter in Python. I got the basic code from here: http://www.python-course.eu/tkinter_entry_widgets.php

    Which provides:

    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)
    
    e1 = Entry(master)
    e2 = Entry(master)
    
    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( )
    

    and modified it for my purposes. My problem is setting a default variable inside the input.

    I have looked all over and it seems as though a common solution is using v = Tkinter.StringVar() and v.set("Default"). However, I am using integers so I changed it to this:

    v = Tkinter.IntVar()
    e1 = Entry(master)
    v.set(pd.value_counts(df['month'].values, sort=False)[1])
    

    where

    In [29]: pd.value_counts(df['month'].values, sort=False)[1]
    Out[29]: 247
    

    My code runs but there is no default in the input.