In this tutorial, I will explain how to take user input and store it in a variable using Python Tkinter. Recently someone asked me how to collect user details like name, address, and email in a Tkinter application and save them to a database. I will share the solution I found during research and provide a step-by-step guide with examples.
Take User Input and Store It in a Variable Using Python Tkinter
Let us learn how to take user input and store it in a variable using Python Tkinter.
Read How to Create Labels in Python with Tkinter?
1. Create the Tkinter GUI
The first step is to create a basic Tkinter window and set up the graphical interface.
import tkinter as tk
from tkinter import messagebox
import sqlite3
import re
# Create the main application window
window = tk.Tk()
window.title("User Input Example")
window.geometry("400x300")tk.Tk() initializes the main window. .title("User Input Example") sets the window title. .geometry("400x300") defines the window size.
Check out How to Create Buttons in Python with Tkinter?
2. Retrieve User Input
We need input fields (Entry widgets) to allow users to enter data.
tk.Label(window, text="Enter your name:").pack()
entry_name = tk.Entry(window)
entry_name.pack()
tk.Label(window, text="Enter your address:").pack()
entry_address = tk.Entry(window)
entry_address.pack()
tk.Label(window, text="Enter your email:").pack()
entry_email = tk.Entry(window)
entry_email.pack()tk.Label() creates a label (text above each input field). tk.Entry() creates an input field for the user. .pack() is used to arrange the elements in the window.
Read How to Create a Menu Bar in Tkinter?
3. Handle Multiple Input Fields
Since we are collecting multiple details (Name, Address, Email), we must retrieve them all when the user submits the form.
def save_input():
name = entry_name.get()
address = entry_address.get()
email = entry_email.get()
if not name or not address or not email:
messagebox.showerror("Error", "All fields are required!")
return.get() method retrieves text from input fields. Validation checks: If any field is empty, it shows an error message. messagebox.showerror() displays a pop-up error.
Read How to Use Tkinter Entry Widget in Python?
4. Validate User Input
Before saving data, we need to ensure the email is valid.
def is_valid_email(email):
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
return re.match(pattern, email) is not None
if not is_valid_email(email):
messagebox.showerror("Error", "Invalid email address!")
returnUses regular expressions (regex) to check if the email format is correct. If the email is invalid, it shows an error message and stops execution.
Check out How to Create Checkboxes in Python Tkinter?
5. Save User Input to a Database
If all validations pass, the user data is stored in a SQLite database.
def create_database():
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
address TEXT,
email TEXT)''')
conn.commit()
conn.close()
def save_input():
name = entry_name.get()
address = entry_address.get()
email = entry_email.get()
if not is_valid_email(email):
messagebox.showerror("Error", "Invalid email address!")
return
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute("INSERT INTO users (name, address, email) VALUES (?, ?, ?)", (name, address, email))
conn.commit()
conn.close()
messagebox.showinfo("Success", "User data saved to database.")
entry_name.delete(0, tk.END)
entry_address.delete(0, tk.END)
entry_email.delete(0, tk.END)sqlite3.connect('users.db') connects to the database (or creates one if it doesn’t exist). Creates a users table if not already present. Saves name, address, and email using INSERT INTO users VALUES (?, ?, ?). Clears input fields after saving.
Read How to Create and Customize Listboxes in Python Tkinter?
6. Add Save Button
The button triggers save_input() when clicked.pady=10 adds space between the button and other elements.
tk.Button(window, text="Save", command=save_input).pack(pady=10)You can see the output in the screenshot below.

Read How to Create Radio Buttons in Python with Tkinter?
Conclusion
In this tutorial, I explained how to take user input and store it in a variable using Python Tkinter. I discussed step by step the process to create the Tkinter GUI, Retrieve user input, handle multiple input fields, validate user input, save user input to a database, and add a save button and by running the whole code we can get the desired output.
You may like to read:
- How to use Tkinter Filedialog in Python
- Expense Tracking Application Using Python Tkinter
- How to Generate Payslip using Python Tkinter

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.