473,785 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = ('R0lGODlhEAAMA KEAAAAAAAC18////////yH5BAEAAAIALAAA AAAQAAwAAAI'
'glIFgyxcfVFsAQ tmS3rjaH1Hg141W aT5ouprt2HHcUgA AOw==')

def test0():
root = Tk()
root.iconbitmap ('@/somepath/window_icon.xbm ')
root.mainloop()

def test1():
root = Tk()
root.bit = BitmapImage(dat a=icon1)
root.iconbitmap (root.bit)
root.mainloop()

def test2():
root = Tk()
bit = BitmapImage(dat a=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__=='__ma in__':
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('w m', 'iconbitmap', self._w, bitmap)
_tkinter.TclErr or: 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 6699
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 = ('R0lGODlhEAAMA KEAAAAAAAC18////////yH5BAEAAAIALAAA AAAQAAwAAAI'
'glIFgyxcfVFsAQ tmS3rjaH1Hg141W aT5ouprt2HHcUgA AOw==')

def test0():
root = Tk()
root.iconbitmap ('@/somepath/window_icon.xbm ')
root.mainloop()

def test1():
root = Tk()
root.bit = BitmapImage(dat a=icon1)
root.iconbitmap (root.bit)
root.mainloop()

def test2():
root = Tk()
bit = BitmapImage(dat a=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__=='__ma in__':
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*********@N0 SP4M.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
4890
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 run into major deficiencies in both Tkinter and PIL (PyGame, wxPython, PyQt, etc. are not really suitable for this program, for a number of reasons, and I have zero interest in discussing why right now). Since the Tkinter.PhotoImage.copy() method doesn't allow a region parameter, I have to save...
9
7073
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 small polygons of various sizes, shapes, colors, shadings, etc. and there are thousands on them on each graph). Have been using MS Excel, but its limitations are unbelievable (only whole number sizes, no way around a 56 color palette, only low quality jpeg files so that when I publish the...
1
3658
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 when I've pre-created the xbm as a string I get a 'can't find your bitmap error' <created my canvas and I draw other objects that are displayed correctly> xbm_string = "#define arrow_width 1 #define arrow_height 16 static char arrow_bits = {
0
2102
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: - This is a common difficulty. - There aren't any good answers. Eventually, I stumbled across a link to this: http://www.tcl.tk/man/tcl8.4/TkCmd/wm.htm#M18
4
6368
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 doing something stupid here, but I don't know what it is. I'm writing an application with a Tkinter GUI (Python 2.4, Tcl/Tk 8.4.) and I want to put a custom icon on the main window. I've followed (so far as I understand it) the recipe in the eff-bot's splendid Introduction to Tkinter - see:...
5
6883
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 paste such an image from the clipboard into a "Text" widget in Tkinter? Here is my first attempt with just trying to print out the image data: ----------------- def pasteImg(tgt): global clipboardEnabled if not clipboardEnabled: return
2
5525
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 : Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 3206, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 3162, in __init__
4
9144
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 any possibility of getting the characteristics of the GIF-picture(or bitmap if i use that)? it would be very helpfull if i for example could do something like canvas.getcolor(image, mouseclick.x,mouseclick.y) if u get the point. get the color of the image where i clicked.
4
3996
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 the event.widget then the bitmap? can i get info about the bitmap then? like color of the pixel i clicked. if so, how? w.bind("<Key>", key) w.bind("<Button-1>", hmm)
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.