How to use a StringVar object in an Entry widget in Tkinter?

A StringVar object in Tkinter helps manage the value of widgets like Entry or Label. You can assign a StringVar object to the textvariable parameter of a widget to dynamically read and update its value.

Basic StringVar Usage

Here's how to create and use a StringVar with an Entry widget ?

from tkinter import *

root = Tk()
root.geometry("400x200")

# Create StringVar object
var = StringVar(root)

# Create Entry widget with StringVar
entry = Entry(root, textvariable=var)
entry.pack(pady=10)

# Set initial value
var.set("Hello World")

root.mainloop()

Getting and Setting Values

Use get() and set() methods to retrieve and modify the StringVar value ?

from tkinter import *

root = Tk()
root.geometry("400x200")

var = StringVar(root)

def display_value():
    # Get current value from StringVar
    current_text = var.get()
    result_label.config(text=f"You entered: {current_text}")

def clear_entry():
    # Set empty value to StringVar
    var.set("")

entry = Entry(root, textvariable=var, width=30)
entry.pack(pady=10)

Button(root, text="Display", command=display_value).pack(pady=5)
Button(root, text="Clear", command=clear_entry).pack(pady=5)

result_label = Label(root, text="", fg="blue")
result_label.pack(pady=10)

root.mainloop()

Complete Example

Here's a complete form example using StringVar with Entry widgets ?

from tkinter import *

root = Tk()
root.geometry("500x300")
root.title("StringVar with Entry Widget")

# Create StringVar objects
name_var = StringVar(root)
email_var = StringVar(root)

def submit():
    name = name_var.get()
    email = email_var.get()
    
    if name and email:
        result = f"Name: {name}\nEmail: {email}"
        result_label.config(text=result, fg="green")
    else:
        result_label.config(text="Please fill all fields!", fg="red")

def clear_form():
    name_var.set("")
    email_var.set("")
    result_label.config(text="")

# Create form layout
Label(root, text="Name:", font=("Arial", 12)).grid(row=0, column=0, padx=10, pady=10, sticky="w")
Entry(root, textvariable=name_var, width=25).grid(row=0, column=1, padx=10, pady=10)

Label(root, text="Email:", font=("Arial", 12)).grid(row=1, column=0, padx=10, pady=10, sticky="w")
Entry(root, textvariable=email_var, width=25).grid(row=1, column=1, padx=10, pady=10)

Button(root, text="Submit", command=submit, bg="lightblue").grid(row=2, column=0, padx=10, pady=20)
Button(root, text="Clear", command=clear_form, bg="lightcoral").grid(row=2, column=1, padx=10, pady=20)

result_label = Label(root, text="", font=("Arial", 10), justify="left")
result_label.grid(row=3, column=0, columnspan=2, padx=10, pady=10)

root.mainloop()

Key Benefits

Method Purpose Example
var.get() Retrieve current value text = var.get()
var.set(value) Update widget value var.set("New text")
textvariable=var Link widget to StringVar Entry(root, textvariable=var)

Conclusion

StringVar objects provide a convenient way to manage Entry widget values dynamically. Use get() to retrieve values and set() to update them programmatically, making form handling much easier in Tkinter applications.

Updated on: 2026-03-26T18:36:23+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements