Frontend Development 14 min read

Python Tkinter GUI Tutorial: Building Windows, Widgets, and Event Handling

This tutorial walks through creating a Tkinter window, adding and configuring common widgets such as labels, buttons, entry fields, comboboxes, checkboxes, radiobuttons, text areas, progress bars, spinboxes, and file dialogs, and demonstrates handling events and customizing widget appearance with concise Python code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Tkinter GUI Tutorial: Building Windows, Widgets, and Event Handling

We start by importing the tkinter package, creating a Tk window, and setting its title.

To display a simple label, we instantiate Label with the window as its parent and call grid to position it.

<code>from tkinter import *
window = Tk()
window.title("First Window")
window.mainloop()</code>

Adding a label widget involves creating a Label instance and placing it with grid :

<code>lbl = Label(window, text="Hello")
lbl.grid(column=0, row=0)</code>

We can change the label's font size using the font parameter:

<code>lbl = Label(window, text="Hello", font=("Arial Bold", 50))</code>

The window size is adjusted with the geometry method, e.g., window.geometry("350x200") .

Buttons are created similarly with Button and positioned via grid . To respond to clicks, define a function and assign it to the button's command attribute.

<code>def clicked():
    lbl.configure(text="Button was clicked!")
btn = Button(window, text="Click Me", command=clicked)
btn.grid(column=1, row=0)</code>

An Entry widget captures user input; its get method retrieves the text, which can be displayed in the label inside the click handler.

<code>txt = Entry(window, width=10)
txt.grid(column=1, row=0)
def clicked():
    res = "Welcome to " + txt.get()
    lbl.configure(text=res)</code>

Focus can be set directly on the entry widget with txt.focus() so the user can type immediately.

A Combobox (from tkinter.ttk ) provides a drop‑down list; values are set via the ['values'] attribute, and the default selection with current() .

<code>from tkinter.ttk import *
combo = Combobox(window)
combo['values'] = (1, 2, 3, 4, 5, "Text")
combo.current(1)
combo.grid(column=0, row=0)</code>

Checkbuttons use a variable such as BooleanVar or IntVar to store their state:

<code>chk_state = BooleanVar()
chk_state.set(True)
chk = Checkbutton(window, text="Choose", var=chk_state)
chk.grid(column=0, row=0)</code>

Radiobuttons share an IntVar variable to ensure only one is selected, and their values can be retrieved with get() :

<code>selected = IntVar()
rad1 = Radiobutton(window, text="First", value=1, variable=selected)
rad2 = Radiobutton(window, text="Second", value=2, variable=selected)
rad1.grid(column=0, row=0)
rad2.grid(column=1, row=0)</code>

A ScrolledText widget provides a multi‑line text area; its size is set with width and height , and text can be inserted or deleted using insert and delete .

<code>from tkinter import scrolledtext
txt = scrolledtext.ScrolledText(window, width=40, height=10)
txt.grid(column=0, row=0)
txt.insert(INSERT, "Text goes here")
txt.delete(1.0, END)</code>

Message boxes are shown via messagebox.showinfo inside a button callback.

<code>from tkinter import messagebox
def clicked():
    messagebox.showinfo("Message title", "Message content")
btn = Button(window, text="Click here", command=clicked)
btn.grid(column=0, row=0)</code>

File dialogs allow users to select files or directories using filedialog.askopenfilename , askopenfilenames , or askdirectory , optionally with initialdir and filetypes arguments.

<code>from tkinter import filedialog
file = filedialog.askopenfilename()
files = filedialog.askopenfilenames()
dir = filedialog.askdirectory()
</code>

Progress bars from tkinter.ttk are created with Progressbar , configured via the value option, and styled using a ttk.Style object.

<code>from tkinter.ttk import Progressbar, Style
style = Style()
style.theme_use('default')
style.configure('black.Horizontal.TProgressbar', background='black')
bar = Progressbar(window, length=200, style='black.Horizontal.TProgressbar')
bar['value'] = 70
bar.grid(column=0, row=0)
</code>

Spinboxes provide numeric input with a defined range ( from_ and to ) or a set of specific values, and can be linked to a variable for default values.

<code>spin = Spinbox(window, from_=0, to=100, width=5)
spin.grid(column=0, row=0)
var = IntVar()
var.set(36)
spin = Spinbox(window, from_=0, to=100, width=5, textvariable=var)
</code>

All examples are combined into complete scripts that create a functional GUI window with the demonstrated widgets and interactions.

GUIpythonTutorialwidgetsTkinter
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.