tkinter icons as bytestrings, not files?

Eric Brunel see.signature at no.spam
Tue Feb 24 11:52:42 EST 2009


On Tue, 24 Feb 2009 05:56:12 +0100, Peter Billam <peter at www.pjb.com.au>  
wrote:

> Greetings,
>
> As a newbie, starting with Python3, I'm working my way through Mark
> Summerfield's tkinter examples.  In the toolbar, there's lines like:
>     for image, command in (
>         ('images/filenew.gif',  self.fileNew),
>         ('images/fileopen.gif', self.fileOpen),
>         ('images/filesave.gif', self.fileSave),
>         ('images/editadd.gif',  self.editAdd),
>         ('images/editedit.gif', self.editEdit),
>         ('images/editdelete.gif', self.editDelete),):
>     image = os.path.join(os.path.dirname(__file__), image)
>     image = tkinter.PhotoImage(file=image)
>     self.toolbar_images.append(image)
>     button = tkinter.Button(toolbar, image=image, command=command)
>     button.grid(row=0, column=len(self.toolbar_images) -1)
>
> which are always failing because the icons are in the wrong place
> and I can't help but wonder if I can put all these little images
> in the application itself, either as b'whatever' or uuencoded or
> in svg etc, and then invoke some suitable variation of the
>     image = tkinter.PhotoImage(file=image)
> or of the
>     button = tkinter.Button(toolbar, image=image, command=command)
> command ?  (I did try help('tkinter.PhotoImage') but didn't spot a
> magic key (unless it's "data=something, format=something")) ...

Just dump the contents of the file as a bytestring and use:
image = PhotoImage(data=the_byte_string)
With no extension, I guess it'll only work with GIF files, as it is the  
only natively supported image format in tcl/tk (AFAIK...).

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list