Working with images in Tkinter
To solve the corporate logo issue and spruce up our application with some icons, we're going to need to understand how to work with images in Tkinter. Tkinter provides access to image files through two classes: the PhotoImage
class and the BitmapImage
class. Let's see how these classes can help us add graphics to our application.
Tkinter PhotoImage
Many Tkinter widgets, including Label
and Button
, accept an image
argument that allows us to display an image on the widget. This argument requires that we create and pass in a PhotoImage
(or BitmapImage
) object.
Making a PhotoImage
object is fairly simple:
myimage = tk.PhotoImage(file='my_image.png')
PhotoImage
is typically called with the keyword argument file
, which is pointed to a file path. Alternatively, you can use the data
argument to point to a bytes
object containing image data. In either case, the resulting object can now be used wherever an image
argument...