Skip to Content
Course content

380: Introduction to tkinter

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Up until now, everything we've built has lived in the terminal. It's great for logic and data processing, but if you want to build something someone can actually "use" without knowing how to run a script, you need a Graphical User Interface (GUI). In Python, the standard way to do this is with tkinter.

How do I actually get a window to show up and stay open?

The most common mistake I see when people start with tkinter is forgetting that a GUI is essentially an infinite loop. If you just create a window and run the script, the program will reach the end of the file and exit immediately, closing your window before you even see it. You have to tell Python to enter the "main loop," which keeps the application listening for events like mouse clicks or key presses.

Here is the bare minimum you need to get a window running. I'll use a simple "Currency Converter" shell as our example:

import tkinter as tk

# This is the main window (the 'root' of your app)
root = tk.Tk()
root.title("Quick Currency Converter")
root.geometry("300x200") # Set a starting size: width x height

# We'll add widgets here later...

# This keeps the window open
root.mainloop()

What's the deal with .pack() vs .grid()?

Creating a widget (like a Label or a Button) isn't enough; you have to tell tkinter where to put it. This is called "geometry management." You'll mostly encounter .pack() and .grid().

  • .pack() is like stacking blocks. It just shoves the widget into the next available space (top, bottom, left, or right). It's fast for simple layouts.
  • .grid() is like a spreadsheet. You specify a row and a column. I almost always use this for anything more complex than a single button because it gives you actual control.

One big warning: never mix .pack() and .grid() in the same parent window. If you do, your program will likely freeze or crash because the two managers will fight over who controls the window size. Pick one and stick with it.

# Using grid for a clean layout
label = tk.Label(root, text="Amount in USD:")
label.grid(row=0, column=0, padx=10, pady=10)

entry = tk.Entry(root)
entry.grid(row=0, column=1, padx=10, pady=10)

How do I make a button actually trigger a function?

Buttons are useless if they don't do anything. To make them work, you use the command argument. This is where a lot of learners trip up: you pass the name of the function, not the result of the function.

If you write command=my_function() with parentheses, Python runs that function the moment the script starts and assigns the return value to the button. You want command=my_function (no parentheses) so that tkinter can call it only when the click happens.

def convert_currency():
    # This is where the logic happens
    usd_amount = entry.get()
    # Let's pretend 1 USD = 0.92 EUR
    result = float(usd_amount) * 0.92
    result_label.config(text=f"Result: {result:.2f} EUR")

# Create the button and link it to the function
calc_button = tk.Button(root, text="Convert", command=convert_currency)
calc_button.grid(row=1, column=0, columnspan=2, pady=10)

# A label to show the output
result_label = tk.Label(root, text="Result: 0.00 EUR")
result_label.grid(row=2, column=0, columnspan=2)



📋 Practical Task

Build a Random Dice Roller

Create a small tkinter application that acts as a digital six-sided die. Your application should meet the following requirements:

  • A window titled "Dice Roller".
  • A Label that initially displays "Roll the die!" or a "0".
  • A Button labeled "Roll!".
  • When the button is clicked, the label should update to show a random integer between 1 and 6.

Hint: You will need to import the random module to generate the number and use the .config(text=...) method to update the label's text dynamically.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.