472,353 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 6394
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark 'Kamikaze' Hughes | last post by:
In the new Python game I'm developing, I need to crop out individual tiles from larger tilesets, and maintain transparency. Unfortunately, I've...
9
by: rhmd | last post by:
I need to create image files (eg bmp or jpeg) of xy scatter graphs (i.e., graphs in which markers denote individual points; the markers need to be...
1
by: Brett | last post by:
I'm trying to load a xbm formatted string as a bitmap and draw it to a canvas....I don't have any trouble with loading/displaying xbm files, it's...
0
by: John Fouhy | last post by:
Hi all, I have been attempting to change the title bar icon of my Tkinter applications in Windows XP. Chasing round in google, I discovered: -...
4
by: Tim Jarman | last post by:
Apologies in advance for the long post - I wanted to be sure I included all the relevant details. The answer is probably very, very simple. I am...
5
by: exhuma.twn | last post by:
As many might know, windows allows to copy an image into the clipboard by pressing the "Print Screen" button on the keyboard. Is it possible to...
2
by: =?iso-8859-1?q?C=E9dric_Lucantis?= | last post by:
Hi, I can only load gif images with Tkinter.PhotoImage and none with BitmapImage. I tried png, jpg, bmp and xpm and always got this errors : ...
4
by: skanemupp | last post by:
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\elmapovic.gif') w.create_image(10, 10, image = mapq, anchor = NW) after doing this is there...
4
by: skanemupp | last post by:
when calling function hmm here, what do i get? the widget i clicked on? if i have a canvs on wich i have a bitmap and i click on the bitmap, is...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.