Monday 20 April 2015

Using images with TKinter

Now is time that we use images for our oncoming projects, first I will start with a simple piece of code, but before that you must make sure to save the script in the same folder in which the images are contained (well, at least with the present code), this images are named "8_01.gif", "8_02.gif", "8_03.gif", "8_04.gif", "8_05.gif":







The code is the next one:

from Tkinter import *

root = Tk()

pic_1 = PhotoImage(file = "8_01.gif")
pic_2 = PhotoImage(file = "8_02.gif")
pic_3 = PhotoImage(file = "8_03.gif")
pic_4 = PhotoImage(file = "8_04.gif")
pic_5 = PhotoImage(file = "8_05.gif")

label = Label(root, image = pic_5)
label.pack()

btn1 = Button(root, image = pic_1)
btn1.pack()

btn2 = Button(root, image = pic_2)
btn2.pack()

btn3 = Button(root, image = pic_3)
btn3.pack()

btn4 = Button(root, image = pic_4)
btn4.pack()

root.mainloop()


Well, you can find more information about the PhotoImage class in the next link: http://effbot.org/tkinterbook/photoimage.htm, the part that I'd like to mention here is that in that article says: "The PhotoImage class can read GIF and PGM/PPM images", which means that if you want to use another type of format, then you should install Python Imaging Library (PIL)
(I just used gif, the other format is unknown for me)
The result is shown in the next picture:


As you all can see, this class allows the use of images in Labels and Buttons, of course that this is just the begining, there are ways in which one could manage the the organization and position of everything.

1 comment:

  1. Golden Nugget Las Vegas - MapyRO
    Information and Reviews about 공주 출장샵 Golden Nugget Las Vegas, 경상남도 출장마사지 Golden Nugget Casino, Las 경상북도 출장마사지 Vegas, 전라북도 출장마사지 Nevada - 하남 출장샵 MapyRO users, download map,

    ReplyDelete