469,328 Members | 1,315 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Tkinter bitmap bug ?

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
Jul 18 '05 #1
2 5891
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

Jul 18 '05 #2
Eric Brunel <er*********@N0SP4M.com> wrote in message news:<bu**********@news-reader5.wanadoo.fr>...
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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mark 'Kamikaze' Hughes | last post: by
reply views Thread by John Fouhy | last post: by
5 posts views Thread by exhuma.twn | last post: by
2 posts views Thread by =?iso-8859-1?q?C=E9dric_Lucantis?= | last post: by
4 posts views Thread by skanemupp | last post: by
4 posts views Thread by skanemupp | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.