CodeGym /Courses /Python SELF EN /Creating a Basic Application Window with Tkinter for User...

Creating a Basic Application Window with Tkinter for User Interaction

Python SELF EN
Level 49 , Lesson 1
Available

1. Creating the Main Application Window

Today we’re kicking off an exciting journey into the world of graphical interfaces with the Tkinter library. After this lecture, your PC won’t just be a boring metal box but a real wizard that not only computes but also shows results to users on the screen. So buckle up, and let’s start building our first basic application window!

Let’s begin by creating the main window of our application. In Tkinter, this is pretty straightforward. We’ll use the Tk() function to create an instance of the main window—it’s like the traditional "Hello, World!" in the land of GUIs.


import tkinter as tk

# Create an application instance
root = tk.Tk()

# Set the window title
root.title("My First Application")

# Start the main loop
root.mainloop()
    

What’s happening here?

  1. Import Tkinter: First, we import the tkinter library. For convenience, we import it as tk (because, let’s face it, we programmers love abbreviations).
  2. Create the window: We create a root object that represents the main application window. Think of this window as its "heart," and without it, our app wouldn’t survive.
  3. Set the window title: Using the title() method, we set the window’s title so users know what they’re dealing with. In this case, we just named it "My First Application."
  4. Main loop (mainloop()): This is the essential part that starts event handling in our app. While this loop runs, our window stays on screen, waiting for user interaction.

Step-by-Step Guide

  1. Create the main window: Every interface begins by creating the main window.

    
    root = tk.Tk()
    root.title("Basic Window")
                

    Yay! You just invited a window into your life. Hope you’ve got curtains!

  2. Customize the window: Let’s add a little personality.

    
    root.geometry("800x600")  # Width x Height
    root.iconbitmap('icon.ico')  # You can specify your own icon file
                
  3. Main application loop: To ensure your window doesn’t vanish as soon as it appears, we run the main loop.

    
    root.mainloop()
                

Main Application Loop

Now that we’ve got a window, let’s talk about the mainloop(). It’s like an invisible DJ at your party—everything runs smoothly while the beats are spinning. The main loop stays constantly alert, keeping the app running and reacting to user actions, like mouse clicks or key presses.

Without this loop, your app would simply show an empty window and quit. So don’t forget to include it at the end of every Tkinter script!

2. Getting to Know Widgets

Tkinter provides a variety of widgets, each with its own unique purpose. Let’s get to know some of them.

Basic Tkinter Widgets

  • Window: The main surface where all other widgets live.
  • Button: An interactive element that responds to clicks.
  • Entry: A text field for single-line text input.
  • Label: Used for displaying text or images.
  • Text: For multi-line text input and display.

Widget Examples

Here’s a small example demonstrating how to create a simple window with a button and a label:


import tkinter as tk

def say_hello():
    label.config(text="Hello, World!")

root = tk.Tk()
root.title("My First Application")

label = tk.Label(root, text="Press the button")
label.pack(pady=10)

button = tk.Button(root, text="Press me", command=say_hello)
button.pack(pady=5)

root.mainloop()
    

When you run this code, you’ll see a window with a label and a button. Click the button—and the label will greet the world!

3. Adding Widgets to the Window

Now that we have our awesome window (yes, for now that’s pretty much all we’ve got), let’s add some widgets. Widgets are user interface elements like buttons, labels, entry fields, etc. Let’s start with something simple: creating a button and a label.


# Import the tkinter library
import tkinter as tk

# Create the main application window
root = tk.Tk()
root.title("My First Application")

# Create a label and place it in the window
label = tk.Label(root, text="Hello, user! Welcome to my first GUI.")
label.pack()  # Use pack method to place the widget

# Create a button and place it in the window
button = tk.Button(root, text="Click me!", command=lambda: print("Button clicked!"))
button.pack()

# Start the main loop
root.mainloop()
    

Explanations

  1. Label: We created a label with the text "Hello, user! Welcome to my first GUI." using Label(). The pack() method at the end is a simple way to place the widget on the screen.

  2. Button: The button is created with the text "Click me!". We also passed a command parameter that specifies what function to call when the button is clicked. Here, the command calls lambda: print("Button clicked!"), so when you click, you’ll see a message in the console.

So now we’ve not only created a basic window but also made it interactive! You could say we’ve gone from just a window to a small "application." This is just the beginning—there’s so much more ahead!

I hope you’ve enjoyed this introductory journey into the world of Tkinter. Remember, GUIs are your chance to show the world your programming skills in a visual way. In the next lecture, we’ll explore adding buttons and entry fields, so get ready for even more interactive interfaces!

1
Task
Python SELF EN, level 49, lesson 1
Locked
Creating a Simple Application Window
Creating a Simple Application Window
2
Task
Python SELF EN, level 49, lesson 1
Locked
Adding widgets to the window
Adding widgets to the window
3
Task
Python SELF EN, level 49, lesson 1
Locked
Using Event Handlers
Using Event Handlers
4
Task
Python SELF EN, level 49, lesson 1
Locked
Creating a Personalized Greeting
Creating a Personalized Greeting
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION