Skip to content
Related Articles

Related Articles

Improve Article

How to Use Bitmap images in Button in Tkinter?

  • Last Updated : 13 Jan, 2021

Prerequisite: Python GUI – tkinter

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python.

A bitmap is an array of binary data representing the values of pixels in an image. A GIF is an example of a graphics image file that has a bitmap.

To create a bitmap image ‘bitmap’ attribute of the button() function is used display. It can take the following values:

  • error
  • gray75
  • gray50
  • gray25
  • gray12
  • hourglass
  • info
  • questhead
  • question
  • warning

Syntax:



Button(…, bitmap=”<value>”)

Approach 1:

  • Import module
  • Create object
  • Create buttons 
  • Execute code

Program:

Python3




# Import Module
from tkinter import *
  
# Create Objects
root = Tk()
  
# Buttons
Button(root, relief=RAISED, bitmap="error").pack(pady=10)
Button(root, relief=RAISED, bitmap="hourglass").pack(pady=10)
Button(root, relief=RAISED, bitmap="info").pack(pady=10)
Button(root, relief=RAISED, bitmap="question").pack(pady=10)
Button(root, relief=RAISED, bitmap="warning").pack(pady=10)
Button(root, relief=RAISED, bitmap="gray75").pack(pady=10)
Button(root, relief=RAISED, bitmap="gray50").pack(pady=10)
Button(root, relief=RAISED, bitmap="gray25").pack(pady=10)
Button(root, relief=RAISED, bitmap="gray12").pack(pady=10)
Button(root, relief=RAISED, bitmap="questhead").pack(pady=10)
  
# Execute Tkinter
root.mainloop()

Output:

Approach 2:

In this method, we will create a list of bitmaps and iterate through all bitmaps while passing them to button() function.

  • Import module
  • Create object
  • Create bitmap list
  • Iterate through the list
  • Create buttons while iterating
  • Execute code

Program:

Python3




# Import Module
from tkinter import *
  
# Create Objects
root = Tk()
  
# Create Bitmaps List
bitmaps = ["error",
           "gray75",
           "gray50",
           "gray25",
           "gray12",
           "hourglass",
           "info",
           "questhead",
           "question",
           "warning"]
  
# Iterate through all bitmap list
for bit in bitmaps:
    Button(root, relief=RAISED, bitmap=bit).pack(pady=10)
  
# Execute Tkinter
root.mainloop()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :