0

I've created an image with PIL, and I'm trying to get it show on a Tkinter GUI, but I get a blank window. Any ideas?

import Tkinter
import Image, ImageTk, ImageDraw

#create an image

image = Image.new('RGBA', (100, 100))
i = 0
draw = ImageDraw.Draw(image)
## draw some lines
for i in range(1,10):
	draw.line((0,i * 10) + image.size, fill=128)

##############

def button_click_exit_mainloop (event):
    event.widget.quit() # this will cause mainloop to unblock.

root = Tkinter.Tk()
root.bind("<Button>", button_click_exit_mainloop)
root.geometry('100x100')

#load the image we created before
tkpi = ImageTk.PhotoImage(image)
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=150,height=150)
label_image.pack()
input()
3
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by vegaseat
0

Try to save your image drawing to a GIF file and then load that file ...

import Tkinter
from PIL import Image, ImageTk, ImageDraw

image_file = "aatest.gif"

#create an image

image = Image.new('RGBA', (200, 200))
draw = ImageDraw.Draw(image)
## draw some lines
for i in range(1,10):
    draw.line((0, i * 10) + image.size, fill=128)

image.save(image_file)

##############

def button_click_exit_mainloop (event):
    event.widget.quit() # this will cause mainloop to unblock.

root = Tkinter.Tk()
root.bind("<Button>", button_click_exit_mainloop)
root.geometry('300x300')

#load the image file we created before
tkpi = ImageTk.PhotoImage(file=image_file)
label_image = Tkinter.Label(root, image=tkpi)
#label_image.place(x=0,y=0,width=150,height=150)
label_image.pack()
#input()

root.mainloop()
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.