3 Replies - 1207 Views - Last Post: 12 January 2014 - 02:10 PM Rate Topic: -----

#1 Chilcone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 13

Image viewer/ images for buttons

Posted 11 January 2014 - 06:19 AM

Hi! I'm currently trying to implement simple image viewer, where you can choose from 2 pictures. I have one menubutton which offers these pictures. After choosing one of the images, the program creates 3 or 5 buttons. I would like to append to each of these buttons different images, so the first button would have one image and the second button would have another image and so on. I've created a function with for loop to create these buttons, but can't move on form that point. I can append one image to all of them, but can't do that with different images.
Thanks for help

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.lists_labels = []

        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 create_labels(self, num):
        self.picture = Image.open("blue.gif")
        self.picture.thumbnail((130,130))
        self.tkim = ImageTk.PhotoImage(self.picture)

        for label in self.lists_labels:
            label.destroy()
        self.lists_labels=[]
        
        for i in range(num):
            but = tk.Button(self.master, image = self.tkim)
            but.grid(row = i + 1, column = 0)
            self.lists_labels.append(but)


myapp = Halabala()


Is This A Good Question/Topic? 0
  • +

Replies To: Image viewer/ images for buttons

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Image viewer/ images for buttons

Posted 11 January 2014 - 08:47 AM

You could use a list of the images:

imgs = ["blue.gif", "green.fig", "3 more"]

Then, instead of creating individual images:
self.tkim = ImageTk.PhotoImage(self.picture)

you could populate another list of images, say self.pimgs, rather than just the names of the images.

Then instead of this:
for i in range(num):
    but = tk.Button(self.master, image = self.tkim)

you would do something like this:
for i in range(num):
    but = tk.Button(self.master, image = self.pimgs[i])

Was This Post Helpful? 1
  • +
  • -

#3 Chilcone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 13

Re: Image viewer/ images for buttons

Posted 12 January 2014 - 01:30 PM

Thank you very much, but after that, will I be able to get access to original images(I mean not as thumbnails)?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Image viewer/ images for buttons

Posted 12 January 2014 - 02:10 PM

I don't quite understand what you are asking but, yes, if you have the image name (and path, if necessary) you can load (create) an Image from this information.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1