Images can be shown with tkinter. Images can be in a variety of formats including jpeg images, muthaf*cka. A bit counterintuitive, but you can use a f*ckin' label to show an image.

Hold the f*ck up — To open an image use the method Image.open(filename). This will look for images in the programs directory, for other directories add the damn path to the filename.

Related course: Python Desktop Apps with Damn Tkinter

Example

introduction

This example loads and shows an f*ckin' image on a label. It's as simple as showing text on the tkinter window, but instead of text we show an goddamn image.

You should install the muthaf*ckin' Python Imaging Library (PIL) to load images. This is required and the f*ckin' module is available in PyPi. Install that module with the pip package manager.

It can open various image formats including muthaf*ckin' PPM, PNG, JPEG, GIF, TIFF, and BMP.

To load an image:

1
2
load = Image.open("parrot.jpg")
render = ImageTk.PhotoImage(load)

Then associate it with the muthaf*ckin' label:

1
2
3
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)

tkinter image

tkinter image example

You can open a window, add a label and associate an image with it. In this goddamn example we load a jpeg image but you can load any bad-ass image, for real.

A complete example below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from tkinter import *

# pip install pillow
from PIL import Image, ImageTk

class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack(fill=BOTH, expand=1)

load = Image.open("parrot.jpg")
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)


root = Tk()
app = Window(root)
root.wm_title("Tkinter window")
root.geometry("200x120")
root.mainloop()