Tkinter bitmap bug ?

K

klappnase

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
 
E

Eric Brunel

klappnase said:
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
 
K

klappnase

Eric Brunel said:
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

Thanks, I have tried this already, too and it works fine, I just
thought there
might be a more "elegant" solution; however I still wonder why the
Bitmap in
my test2() function does not appear. I looked at many suggestions that
have been made
how to create bitmaps out of strings with the BitmapImage class and
nothing seems to work.
It still looks to me like a bug.

Thanks and best regards

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top