PIL/tkinter - updating label image with button images

3322
2
01-15-2014 06:37 AM
MarekCreative
New Contributor
I'm trying to implement my simple image viewer, where I have one main label and menubutton with three choices. Each of them creates different numbers of buttons with pictures inside them. I would like create one main label and then update its image with images on the buttons. So when I click on the first button with image, that image will appear on the label, then click second button and that second image will appear and so on. I've tried to create two functions one for creating label and updating its image and the second for getting current image on the button, but I wasn't able to do the second one correctly. If u don't understand anything just write me.
Appreciate your help. Here's the code:

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk 

from functools import partial
from PIL import Image, ImageTk

class Halabala():
    def __init__(self):
        self.master = tk.Tk()
        self.master.geometry("1100x700")
        self.pictures = ["pavuk1.gif", "pavuk2.gif", "pavuk3.gif"]
        self.pictures2 = ["cukor1.gif", "cukor2.gif", "cukor3.gif", "cukor4.gif", "cukor5.gif"]

        self.lists_images = []
        self.lists_images2 = []

        self.init_image_list()

        self.lists_labels = []

        #self.main_label = tk.Label(self.master, image = None).grid(row = 0, column = 0) 
        #image with original size 

        self.rbutton = tk.Menubutton(self.master, text = "Choose picture")
        self.picks2 = tk.Menu(self.rbutton)
        self.rbutton.config(menu=self.picks2)
        self.picks2.add_command(label = "Spider", command = partial(self.create_labels,3))
        self.picks2.add_command(label = "Sugar", command = partial(self.create_labels,5))
        self.rbutton.config(width = 30, bg = "white", bd = 5, relief = tk.RAISED)
        self.rbutton.place(x = 900, y = 30)
        self.master.mainloop()

    def init_image_list(self):
        for i in self.pictures:
                picture = Image.open(i)
                picture.thumbnail((130, 130))
                self.lists_images.append(ImageTk.PhotoImage(picture))

        for i in self.pictures2:
                picture = Image.open(i)
                picture.thumbnail((130, 130))
                self.lists_images2.append(ImageTk.PhotoImage(picture))


    def create_labels(self, num):

        for label in self.lists_labels:
            label.destroy()
        self.lists_labels=[]

        for i in range(num):
            if num == 3:
                but = tk.Button(self.master, image = self.lists_images) #command = self.get_command
                but.grid(row = 2, column = i + 1) #label will be in the first row
                self.lists_labels.append(but)
            else:
                but = tk.Button(self.master, image = self.lists_images2)
                but.grid(row = 2, column = i + 1)
                self.lists_labels.append(but)

    #def get_command(self):
        #self.main_label = tk.Label(self.master, image = self.get_image).grid(row = 0, column = 0)

    #def get_image(self):
    # don't know how to get current image from button

myapp = Halabala()
Tags (2)
0 Kudos
2 Replies
JasonScheirer
Occasional Contributor III
You'd probably get better answers to general Python questions like this over on Stack Overflow.
0 Kudos
MarekCreative
New Contributor
Thanks, I'll try.
0 Kudos