Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating your first GUI application using Python and Tkinter can be both exciting and a great way to introduce yourself to graphical interfaces in Python. In this tutorial, we'll create a basic application where a user can enter their name and upon clicking a button, will be greeted with a "Hello, [Name]!" message.
Setting up the basic GUI with Tkinter:
Start by initializing a basic window using tkinter
.
import tkinter as tk root = tk.Tk() root.title("First Tkinter App") root.geometry("300x150")
Creating Widgets:
# Creating a label to guide user instruction_label = tk.Label(root, text="Enter your name:") instruction_label.pack(pady=10) # Entry widget to accept name input name_entry = tk.Entry(root) name_entry.pack(pady=5) # Function to update greeting label when button is clicked def greet_user(): name = name_entry.get() greeting_label.config(text=f"Hello, {name}!") # Button to trigger greeting greet_button = tk.Button(root, text="Greet", command=greet_user) greet_button.pack(pady=10) # Label to display greeting greeting_label = tk.Label(root, text="") greeting_label.pack(pady=10)
Run the main loop:
This will display the window and keep it running until the user closes it.
root.mainloop()
Now, put all the code snippets together and run your script. You'll get a simple Tkinter window where you can enter your name, click the "Greet" button, and receive a personalized greeting.
This basic example lays the foundation for creating more complex GUI applications using Python and Tkinter. You can expand upon this by introducing more widgets, event handlers, and even integrate with other Python modules to make fully-featured applications.
How to create a simple GUI application in Python:
import tkinter as tk def on_button_click(): label.config(text="Hello, " + entry.get()) # Create the main window root = tk.Tk() root.title("Simple GUI Application") # Create and place widgets (Button, Entry, Label) entry = tk.Entry(root) entry.pack(pady=10) button = tk.Button(root, text="Click Me", command=on_button_click) button.pack(pady=10) label = tk.Label(root, text="") label.pack(pady=10) # Run the GUI application root.mainloop()
Getting started with Tkinter for GUI development:
import tkinter as tk # Create the main window root = tk.Tk() root.title("Getting Started with Tkinter") # Run the GUI application root.mainloop()
Building your first Python GUI with Tkinter:
import tkinter as tk # Create the main window root = tk.Tk() root.title("My First Tkinter GUI") # Run the GUI application root.mainloop()
Creating a basic window in Python Tkinter:
import tkinter as tk # Create the main window with title and size root = tk.Tk() root.title("Basic Tkinter Window") root.geometry("300x200") # Set window size # Run the GUI application root.mainloop()
Adding buttons and labels to Tkinter GUI:
import tkinter as tk def on_button_click(): label.config(text="Hello, " + entry.get()) # Create the main window root = tk.Tk() root.title("Buttons and Labels in Tkinter") # Create and place widgets (Button, Entry, Label) entry = tk.Entry(root) entry.pack(pady=10) button = tk.Button(root, text="Click Me", command=on_button_click) button.pack(pady=10) label = tk.Label(root, text="") label.pack(pady=10) # Run the GUI application root.mainloop()
Python Tkinter entry widget for user input:
Entry
widget to allow users to input text in a GUI application.import tkinter as tk def on_button_click(): label.config(text="Hello, " + entry.get()) # Create the main window root = tk.Tk() root.title("Tkinter Entry Widget") # Create and place widgets (Entry, Button, Label) entry = tk.Entry(root) entry.pack(pady=10) button = tk.Button(root, text="Click Me", command=on_button_click) button.pack(pady=10) label = tk.Label(root, text="") label.pack(pady=10) # Run the GUI application root.mainloop()
Handling events in Tkinter GUI applications:
import tkinter as tk def on_button_click(): label.config(text="Hello, " + entry.get()) # Create the main window root = tk.Tk() root.title("Handling Events in Tkinter") # Create and place widgets (Entry, Button, Label) entry = tk.Entry(root) entry.pack(pady=10) button = tk.Button(root, text="Click Me", command=on_button_click) button.pack(pady=10) label = tk.Label(root, text="") label.pack(pady=10) # Run the GUI application root.mainloop()