Tkinter bitmap bug ?

klappnase klappnase at web.de
Wed Jan 21 12:14:51 EST 2004


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.
Running test1() returns following error message:

Traceback (most recent call last):
  File "/usr/local/share/test.py", line 52, in ?
    test1()
  File "/usr/local/share/test.py", line 20, in test1
    root.iconbitmap(root.bit)
  File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1448, in wm_iconbitmap
    return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "pyimage1" not defined

No surprise - the image gets garbage collected as soon as it is created, I know.
So I tried test2() to avoid this. Unfortunately it is quite the same here,
and this is the point where it begins to look like a bug to me. Running test2()
returns a window with only one button with the PhotoImage correctly displayed.
Obviously the PhotoImage does not get garbage collected, so why does the 
BitmapImage ???

I thought then if this all would not work I might use a PhotoImage as window
icon using the iconwindow() method - see test3().
However the icon does not show up.

Any help on this would be greatly appreciated.

Thanks in advance

Michael



More information about the Python-list mailing list