How to Use Bitmap images in Button in Tkinter? (original) (raw)
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=””)
Approach 1:
- Import module
- Create object
- Create buttons
- Execute code
Program:
Python3
from
tkinter
import
*
root
=
Tk()
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
)
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
from
tkinter
import
*
root
=
Tk()
bitmaps
=
[
"error"
,
`` "gray75"
,
`` "gray50"
,
`` "gray25"
,
`` "gray12"
,
`` "hourglass"
,
`` "info"
,
`` "questhead"
,
`` "question"
,
`` "warning"
]
for
bit
in
bitmaps:
`` Button(root, relief
=
RAISED, bitmap
=
bit).pack(pady
=
10
)
root.mainloop()
Output: