Tkinter bitmap bug ?

Eric Brunel eric.brunel at N0SP4M.com
Wed Jan 21 13:16:09 EST 2004


klappnase wrote:
> Hello everyone,
> 
> I have seen several threads here on this problem, but still cannot figure out
> the solution. I want to create a window icon for a Tkinter application and tried
> several things that are supposed to work. Here is a little demo of what I tried:
> 
> #################################from Tkinter import *
> 
> icon1 = ('''#define im_width 26
> #define im_height 25
> static char im_bits[] = {
> 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0x7e,
> 0x00,0x00,0xe0,0x7f,0x00,0x00,0xff,0x63,0x00,0x00,0x3f,0x70,0x00,0x00,0x03,
> 0x7e,0x00,0x00,0xe3,0x7f,0x00,0x00,0xff,0x63,0x00,0x00,0x3f,0x60,0x00,0x00,
> 0x03,0x60,0x00,0x00,0x03,0x60,0x00,0x00,0x03,0x78,0x00,0x00,0x03,0x7c,0x00,
> 0x00,0x03,0x7e,0x00,0xc0,0x03,0x7e,0x00,0xe0,0x03,0x3c,0x00,0xf0,0x03,0x18,
> 0x00,0xf0,0x03,0x00,0x00,0xe0,0x01,0x00,0x00,0xc0,0x00,0x00,0x00,0x00,0x00,
> 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };''')
> 
> icon2 = ('R0lGODlhEAAMAKEAAAAAAAC18////////yH5BAEAAAIALAAAAAAQAAwAAAI'
>         'glIFgyxcfVFsAQtmS3rjaH1Hg141WaT5ouprt2HHcUgAAOw==')
> 
> def test0():
>     root = Tk()
>     root.iconbitmap('@/somepath/window_icon.xbm')
>     root.mainloop()
> 
> def test1():
>     root = Tk()
>     root.bit = BitmapImage(data=icon1)
>     root.iconbitmap(root.bit)
>     root.mainloop()
> 
> def test2():
>     root = Tk()
>     bit = BitmapImage(data=icon1)
>     img = PhotoImage(data=icon2)
>     try:
>         b1 = Button(root, bitmap=bit)
>         b1.icon_ref = bit
>         b1.pack()
>     except:
>         pass
>     try:
>         b2 = Button(root, image=img)
>         b2.icon_ref = img
>         b2.pack()
>     except:
>         pass
>     root.mainloop()
> 
> def test3():
>     root = Tk()
>     img = PhotoImage(data=icon2)
>     top = Toplevel(root)
>     l = Label(top, image=img)
>     l.icon_ref = img
>     l.pack()
>     root.iconwindow(top)
>     root.mainloop()
> 
> if __name__=='__main__':
>     test0()
>    
> #############################################
> 
> Running the test0() function works fine, however I wanted to not to carry an
> external bitmap file around, so I tried to pass the bitmap data to a BitmapImage
> object.

As you saw, this won't work: the only way I found to set an iconbitmap on a 
window is using the '@/path/to/file' syntax. Why not keep the data for your 
image in the code and generate a temporary file from this data? Then you can use 
the '@...' stuff without having to carry an external file everywhere. Just don't 
forget to delete the file when your application exits. We do that all the time 
and it works quite fine.

HTH
-- 
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com




More information about the Python-list mailing list