473,729 Members | 2,122 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 6692
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
4889
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
3655
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
2098
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
6363
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
6874
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
5519
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
9141
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
3994
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)
0
8931
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8767
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9221
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
9162
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8165
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...
0
6028
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
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3246
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
2701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.