To create a GUI window with a button that changes a label to say “Hello, World!” when clicked, you can use Python’s tkinter
library. Below is a complete example:
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("Hello World App")
window.geometry("300x200")
# Create a label
label = tk.Label(window, text="")
label.pack(pady=20)
# Define the button click event function
def on_button_click():
label.config(text="Hello, World!")
# Create a button
button = tk.Button(window, text="Click Me", command=on_button_click)
button.pack(pady=10)
# Run the application
window.mainloop()
To run this code, you need to have Python installed on your machine. Save the code to a file, for example, hello_world.py
, and run it using the command python hello_world.py
from your command line or terminal. This will open a window with a button that says “Click Me”. When you click the button, the label will update to display “Hello, World!”.
Let’s go through the program step by step, explaining each part and the necessary concepts to understand it.
1. Importing the Tkinter Module
tkinter
is a standard Python library used for creating graphical user interfaces (GUIs).tk
is an alias for thetkinter
module, allowing us to reference its components usingtk
instead oftkinter
.
2. Creating the Main Window
window = tk.Tk()
window.title("Hello World App")
window.geometry("300x200")
tk.Tk()
initializes a new Tkinter window.window.title("Hello World App")
sets the title of the window to “Hello World App”.window.geometry("300x200")
sets the size of the window to 300 pixels wide by 200 pixels high.
3. Creating a Label
label = tk.Label(window, text="")
label.pack(pady=20)
tk.Label(window, text="")
creates a label widget in thewindow
with no initial text.label.pack(pady=20)
adds the label to the window and places it with a vertical padding (space) of 20 pixels.
4. Defining the Button Click Event Function
def on_button_click():
label.config(text="Hello, World!")
def on_button_click():
defines a function namedon_button_click
.label.config(text="Hello, World!")
changes the text of the label to “Hello, World!” when this function is called.
5. Creating a Button
button = tk.Button(window, text="Click Me", command=on_button_click)
button.pack(pady=10)
tk.Button(window, text="Click Me", command=on_button_click)
creates a button widget in thewindow
with the text “Click Me”. When the button is clicked, it calls theon_button_click
function.button.pack(pady=10)
adds the button to the window and places it with a vertical padding of 10 pixels.
6. Running the Application
window.mainloop()
window.mainloop()
starts the Tkinter event loop, which waits for user interaction and updates the GUI as needed. This line keeps the window open until the user closes it.
Key Concepts to Learn and Understand
- Tkinter Basics:
- Tkinter is a standard Python library for creating GUIs.
- Widgets are the building blocks of a Tkinter GUI (e.g., windows, labels, buttons).
- Creating a Window:
- Initializing the main window using
tk.Tk()
. - Setting the window title and size.
- Initializing the main window using
- Widgets:
- Label: Used to display text or images.
- Button: Used to perform an action when clicked.
- Packing widgets using the
pack()
method to arrange them in the window.
- Event Handling:
- Defining functions to handle events (e.g., button clicks).
- Using the
command
parameter of theButton
widget to link a function to the button click event.
- Configuring Widgets:
- Changing widget properties dynamically using methods like
config()
.
- Changing widget properties dynamically using methods like