Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Tkinter Tutorial – Using Tkinter Buttons

  • Vijaykrishna RamVijaykrishna Ram
  • May 8, 2020
  • Tkinter
Tkinter Tutorial – Using Tkinter Buttons

Hello everyone! In today’s article on Tkinter, we’ll cover the Tkinter Button widget.

The Tkinter button widget is a very simple and easy-to-use widget.

Let’s look at how we can construct buttons and add functionality to our GUI application, using some illustrative examples!


Tkinter Button Widget

This widget can be used to make different types of buttons.

We can have buttons containing not only text, but also images!

We can also make a Tkinter Button call a specific function corresponding to the button functionality, using a callback function.

To define a button with some specific text and a callback function, we can use the following definition:

button = tk.Button(master, text, command)

Here, text is the text on the button, and command refers to the callback function which is invoked when the button is pressed.

Let’s now make a simple application having a button, which when clicked, displays an Alert Box, which tells us that it was clicked!

The below code snippet shows this, with comments explaining every single line of code.

import tkinter as tk
from tkinter import messagebox

# Create the master object
master = tk.Tk()

# Sets the window size as "300x300"
master.geometry("300x300")

# This is the button callback function
# This must be visible to the button, so we must define it before the button widget!
def buttonCallback():
    messagebox.showinfo("Message", "You have clicked the Button!")

# Create a Button widget
button = tk.Button(master, text="Click", command=buttonCallback)

# And a label for it
label_1 = tk.Label(master, text="Simple Button")

# Use the grid geometry manager to put the widgets in the respective position
label_1.grid(row=0, column=0)
button.grid(row=1, column=0)

# The application mainloop
tk.mainloop()

Now, on running this program, you’ll get the following output.

Button Example 1 2
Button Example 1

Now, if you want an image to go along with your button, to make it a bit more fancy, it’s possible!

Simply add another keyword argument to the tk.Button() call, called image! This is a ImageTk.PhotoImage, which you must instantiate using an image file.

To use ImageTk, we must import it from the PIL module. We first create a PIL image, resize it so that it is small enough, and then form our PhotoImage.

from PIL import Image, ImageTk

# Create a tk.PhotoImage
my_image = Image.open("path/to/image.png")
# Resize it to fit button dimensions
my_image = my_image.resize((25, 25), Image.ANTIALIAS)

Now, we’re ready to form the photoimage object.

my_image = Imagetk.PhotoImage(my_image)

Now, finally, we can add it to our button!

b = Button(compound=LEFT, image=my_image, text="Button")

However, you must be a bit careful, since using an icon will override any text that a button has.

To avoid this, use the compound keyword argument. This value can be set to tk.CENTER, tk.LEFT, tk.RIGHT, tk.TOP or tk.BOTTOM. This will specify the orientations of the text, with respect to the image.

Since we will place the text on top of the image, we’ll use tk.TOP

b = Button(compound=LEFT, image=my_image, text="Button", compound=tk.TOP)

I’ll show you the complete code below:

import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk

# Create the master object
master = tk.Tk()

# Sets the window size as "300x300"
master.geometry("300x300")

# This is the button callback function
# This must be visible to the button, so we must define it before the button widget!
def buttonCallback():
    messagebox.showinfo("Message", "You have clicked the Button!")

# Create a tk.PhotoImage
my_image = Image.open("debian.png")
# Resize it to fit button dimensions
my_image = my_image.resize((25, 25), Image.ANTIALIAS)
# Finally, get the PhotoImage object
my_image = ImageTk.PhotoImage(my_image)

# Create a Button widget
button = tk.Button(master, text="Click", image=my_image, command=buttonCallback, compound=tk.TOP)

# And a label for it
label_1 = tk.Label(master, text="Simple Button")

# Use the grid geometry manager to put the widgets in the respective position
label_1.grid(row=0, column=0)
button.grid(row=1, column=0)

# The application mainloop
tk.mainloop()

Output

Button Example 2
Button Example 2

Conclusion

Hopefully, you were also able to build your own Tkinter Button widget. In the upcoming tutorials, we’ll focus more on some more widgets, so stay tuned for more!


Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Convert string to datetime using Python strptime()
Previous Post Convert string to datetime using Python strptime()
Next Post A Guide to using Python Itertools Module
A Guide to using Python Itertools Module

Related Posts

Tkinter IntVar – Tkinter Tutorial with Examples

Tkinter IntVar – Tkinter Tutorial with Examples

  • July 5, 2026
Tkinter StringVar with Examples – Tkinter Tutorial

Tkinter StringVar with Examples – Tkinter Tutorial

  • July 5, 2026
Tkinter GUI Widgets

Tkinter GUI Widgets

  • April 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.