Tkinter is the standard Python library for creating Graphical User Interface (GUI) applications. With Tkinter, you can easily create buttons, labels, input fields, and other widgets for user interaction. In this article, we will explore how to add buttons, labels, and input fields to a Tkinter window and handle user input.
Before adding widgets like buttons, labels, and input fields, we need to set up a basic Tkinter window. Here's how to create a simple window:
import tkinter as tk # Create the main window window = tk.Tk() # Set the window title window.title("Tkinter Widgets Example") # Set the window size window.geometry("400x300") # Start the Tkinter event loop window.mainloop()
This code creates a window titled "Tkinter Widgets Example" with a size of 400x300 pixels. The mainloop()
function starts the Tkinter event loop, allowing the window to remain open and responsive to user actions.
Labels are used to display text in a Tkinter window. You can create a label using the Label
widget and place it in the window using the pack()
, grid()
, or place()
methods. Here's how to add a label to the window:
import tkinter as tk # Create the main window window = tk.Tk() # Create a label widget label = tk.Label(window, text="Welcome to Tkinter!") label.pack() # Add the label to the window # Start the Tkinter event loop window.mainloop()
In this example, the Label
widget displays the text "Welcome to Tkinter!" in the window. The pack()
method is used to place the label inside the window.
Buttons are interactive elements that allow the user to trigger actions when clicked. You can create a button using the Button
widget. To define the behavior of the button, use the command
parameter to specify a function that is called when the button is clicked.
import tkinter as tk # Define the function to be called when the button is clicked def on_button_click(): label.config(text="Button clicked!") # Create the main window window = tk.Tk() # Create a label widget label = tk.Label(window, text="Click the button:") label.pack() # Create a button widget button = tk.Button(window, text="Click Me", command=on_button_click) button.pack() # Start the Tkinter event loop window.mainloop()
In this example, when the user clicks the button, the on_button_click()
function is triggered, which updates the label to say "Button clicked!". The command
parameter is used to bind the button to the function.
Input fields allow users to enter data. In Tkinter, input fields are created using the Entry
widget. You can retrieve the text entered by the user by calling the get()
method on the Entry
widget.
import tkinter as tk # Define the function to display user input def display_input(): user_input = entry.get() label.config(text=f"You entered: {user_input}") # Create the main window window = tk.Tk() # Create a label widget label = tk.Label(window, text="Enter something:") label.pack() # Create an input field (Entry widget) entry = tk.Entry(window) entry.pack() # Create a button widget button = tk.Button(window, text="Submit", command=display_input) button.pack() # Start the Tkinter event loop window.mainloop()
In this example, the user enters text into the input field. When the "Submit" button is clicked, the display_input()
function is called, which retrieves the text entered by the user and displays it in the label.
You can combine labels, buttons, and input fields to create interactive applications. Here's an example of a simple calculator that adds two numbers:
import tkinter as tk # Define the function to calculate the sum of two numbers def calculate_sum(): num1 = float(entry1.get()) num2 = float(entry2.get()) result = num1 + num2 label.config(text=f"Result: {result}") # Create the main window window = tk.Tk() # Create labels for the input fields label1 = tk.Label(window, text="Enter the first number:") label1.pack() # Create the first input field entry1 = tk.Entry(window) entry1.pack() # Create labels for the input fields label2 = tk.Label(window, text="Enter the second number:") label2.pack() # Create the second input field entry2 = tk.Entry(window) entry2.pack() # Create a button to calculate the sum button = tk.Button(window, text="Calculate", command=calculate_sum) button.pack() # Create a label to display the result label = tk.Label(window, text="Result:") label.pack() # Start the Tkinter event loop window.mainloop()
This simple calculator allows the user to enter two numbers and calculates their sum when the "Calculate" button is clicked. The result is displayed in the label below the button.
In this article, we have learned how to add buttons, labels, and input fields to a Tkinter window. Tkinter provides a simple way to build interactive applications with a graphical interface. You can easily create user-friendly applications by combining these widgets and handling user input and actions.