In this tutorial, we will learn how to make a button in Python 3 using the popular GUI (Graphical User Interface) module Tkinter. This module allows you to create various types of buttons that can interact with the user, making your programs more interactive and visually appealing.
We will create a simple application with a button that, when clicked, displays a message on the screen.
Step 1: Install Tkinter
To make a button in Python 3, we will use the Tkinter module, which is included in the standard library for Python 3.x. If you are using Python 2.x, you need to install the module using the following command:
1 |
pip install python-tk |
For Python 3.x, you do not need to install any additional packages.
Step 2: Import Tkinter Module
In your Python script, start by importing the Tkinter module.
1 |
import tkinter as tk |
This will allow you to access all the Tkinter classes, methods, and functionalities in your script.
Step 3: Create the Application Window
Next, we need to create a window for our application, where our button will be displayed. To do this, you can use the following code:
1 2 3 4 5 6 7 8 |
# Create a window window = tk.Tk() # Set the window title window.title("Button Example") # Set the window size window.geometry("300x100") |
This code will create a window with the title “Button Example” and a size of 300×100 pixels.
Step 4: Create the Button
Now that we have our application window, let’s create a button. To do this, we will use the Button class from the Tkinter module and add it to our window.
First, let’s define a function that will be called when the button is clicked. This function will display a message on the screen:
1 2 |
def on_button_click(): print("Button clicked!") |
Now, let’s create the button and place it in the window:
1 2 3 4 5 |
# Create a button button = tk.Button(window, text="Click me!", command=on_button_click) # Place the button in the window button.pack(pady=10) |
The Button class takes several arguments, including the parent window (in our case, “window”), the text to be displayed on the button, and a command that will be executed when the button is clicked.
The pack() method is used to place the button in the window. We added a padding of 10 pixels on the y-axis to provide some space between the button and the window borders.
Step 5: Run the Application Loop
Finally, we need to start the application loop, which will display the window and handle user events like button clicks:
1 2 |
# Run the application loop window.mainloop() |
Now, when you run the script, you should see a window with a button displaying “Click me!”. When you click the button, the message “Button clicked!” will be displayed in the console.
Full Code
Here is the complete code for our button example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import tkinter as tk def on_button_click(): print("Button clicked!") # Create a window window = tk.Tk() window.title("Button Example") window.geometry("300x100") # Create a button button = tk.Button(window, text="Click me!", command=on_button_click) # Place the button in the window button.pack(pady=10) # Run the application loop window.mainloop() |
Output
When you run the script, you will see the application window with a button:
When you click the button, the message “Button clicked!” is displayed in the console.
Conclusion
In this tutorial, we learned how to create a simple button in Python 3 using the Tkinter module.
You can now use this knowledge to create more complex applications with buttons and other graphical components to make your Python programs more interactive and engaging.