How To Make A Button In Python

In this tutorial, we will learn how to create a simple button in Python using Tkinter, the de facto Python library for creating graphical user interfaces (GUIs). By the end of this tutorial, you will understand how to create buttons, bind functions to buttons, and set button properties to customize your buttons in a Tkinter application.

Step 1: Install Tkinter

Tkinter comes pre-installed with Python installations on Windows and macOS. However, if you’re using Linux, you may need to install it manually. Open a terminal and run the following command suited for your Linux distro:

  • For Debian-based systems (like Ubuntu):
    sudo apt-get install python3-tk
  • For Red Hat-based systems (like Fedora):
    sudo yum install tkinter

Step 2: Import Tkinter

First, we need to import the Tkinter library in our Python script, like this:

Step 3: Create a Basic Tkinter Window

Before adding a button, let’s create a simple Tkinter window to hold our button. Here’s how you can create a window:

The Tk() function creates the main window, and the mainloop() function keeps the window displayed on the screen.

Step 4: Create a Button

Now, let’s create a button inside the window. The following code snippet creates a button with the label “Click me!” inside the main window:

The first line creates a button inside the “root” window and assigns it the text “Click me!”. The second line places the button within the window using the pack() geometry manager.

Step 5: Add a Function to the Button

Let’s write a simple function that will be called when the button is clicked. This function will print “Button clicked!” to the console.

Now, let’s bind this function to the button using the command parameter:

Step 6: Customize your Button

You can customize your button’s appearance by setting different properties such as font, background color, and size. Here’s an example of how to set some of these properties:

The bg parameter sets the button’s background color, fg sets the foreground (text) color, font sets the font and size of the text, and height and width set the dimensions of the button.



Creating a button in Python using Tkinter is a simple and straightforward process. You can use these steps to add buttons to your Python GUI applications and customize them by changing text, colors, size, and more. With this knowledge, you can easily incorporate buttons into more complex applications to enhance user interaction.