473,231 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

[Tkinter] LONG POST ALERT: Setting application icon on Linux

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:
http://www.pythonware.com/library/tk...on-methods.htm
- but it isn't working for me. Clearly there#s something I didn't get.

I need a solution for Linux initially (Mandrake 10.1/KDE 3.2 if it makes a
difference) and maybe OS/X in the future. The only solutions I've founds on
Google are Windows-specific.

Here's what I have at the moment, reduced to its essentials:

<code>
# Display an application icon.

import os.path
import Tkinter as tk

APP_NAME = "Icon Test"
HOME = "/home/tim/Projects/tkDev/playwright"
def main():
"Main entry point for the application."
# Create the root window.
root = tk.Tk()
root.title(APP_NAME)

# Set the icon.
print "current icon name = ", root.iconname()
icon_path = os.path.join(HOME, "icon.gif")
print "icon_path =", icon_path
try:
icon_image = tk.PhotoImage(file=icon_path)
print "icon_image =", icon_image
icon_label = tk.Label(image=icon_image)
print "icon_label =", icon_label
assert icon_label.master is root
print "about to fail.."
root.iconwindow(icon_label)
print "success??!"
except IOError:
pass

# Create and show the main window.
root.mainloop()
# Bootstrap code.
if __name__ == "__main__":
main()
</code>

And here's the output I get when I run it:

<output>
current icon name =
icon_path = /home/tim/Projects/tkDev/playwright/icon.gif
icon_image = pyimage1
icon_label = .1076669484
about to fail..
Traceback (most recent call last):
File "test_icon.py", line 38, in ?
main()
File "test_icon.py", line 27, in main
root.iconwindow(icon_label)
File "/usr/local/lib/python2.4/lib-tk/Tkinter.py", line 1473, in
wm_iconwindow
return self.tk.call('wm', 'iconwindow', self._w, pathName)
_tkinter.TclError: can't use .1076669484 as icon window: not at top level
</output>

Obviously the root window doesn't even get displayed.

I don't understand the error message. How can I make the Label "top level"
enough to do the job? It's a child of root. Calling .pack() doesn't seem to
help (although if I comment out the iconwindow() call so that the window
actually appears, I can see that the GIF file has loaded correctly).

Do I need to hide the root window and create a new Toplevel for my app's
main window? I thought root would be an instance of Toplevel. As you can
tell, I'm a bit confused here!

Wishing-I-was-doing-this-in-wxPython-ly,

Tim J

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #1
4 6308
Here is a short program that sets Tk's window icon on Linux. My window
manager is icewm, and it uses a scaled version of the "flagup" image
both at the upper-left corner of the window and on the task bar entry
for the window.

import Tkinter
app = Tkinter.Tk()
app.iconbitmap("@/usr/X11R6/include/X11/bitmaps/flagup")
app.mainloop()

As often happens, the Tkinter documentation doesn't tell the whole
story---you have to dig into the Tk documentation. I started with "man
n wm", and read the following:
If bitmap is specified, then it names a bitmap in the standard
forms accepted by Tk (see the Tk_GetBitmap manual entry for
details).
OK, on to Tk_GetBitmap...
@fileName FileName must be the name of a file containing a
bitmap description in the standard X11 or X10 format.
and I happened to know that some bitmaps in this format exist in the
directory I mentioned above. Note that the "standard X11 format" is
monochrome, so you will not be able to use color images with
"iconbitmap" on Linux. Tk doesn't support _NET_WM_ICON for setting
full-color icons.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCRt2dJd01MZaTXX0RAj17AJ4k30Z3X87YeBY+J59GvR RNZ01bdACdFJPA
IogJMISWv4+m8dySTalR15g=
=bQtD
-----END PGP SIGNATURE-----

Jul 18 '05 #2
Jeff Epler wrote:
Here is a short program that sets Tk's window icon on Linux. My window
manager is icewm, and it uses a scaled version of the "flagup" image
both at the upper-left corner of the window and on the task bar entry
for the window.

import Tkinter
app = Tkinter.Tk()
app.iconbitmap("@/usr/X11R6/include/X11/bitmaps/flagup")
app.mainloop()

As often happens, the Tkinter documentation doesn't tell the whole
story---you have to dig into the Tk documentation. I started with "man
n wm", and read the following:
If bitmap is specified, then it names a bitmap in the standard
forms accepted by Tk (see the Tk_GetBitmap manual entry for
details).
OK, on to Tk_GetBitmap...
@fileName FileName must be the name of a file containing a
bitmap description in the standard X11 or X10 format.
and I happened to know that some bitmaps in this format exist in the
directory I mentioned above. Note that the "standard X11 format" is
monochrome, so you will not be able to use color images with
"iconbitmap" on Linux. Tk doesn't support _NET_WM_ICON for setting
full-color icons.

Jeff


Thanks for this, Jeff - I'll do some digging in the Tk docs. My problem is
that I'm trying to use iconwindow() to use a colour image, as opposed to
iconbitmap(), although if push comes to shove I suppose I could use that.

Thanks again for the quick response - on Easter weekend too!

Tim J

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #3
I have written a rather hackish extension to use NET_WM_ICON to set
full-color icons in Tkinter apps. You can read about it here:
http://craie.unpy.net/aether/index.c...re/01112237744
you'll probably need to take a look at the EWMH spec, too. If KDE
supports NET_WM_ICON, this may work for you (but you'll have to convert
your image manually to the format required for NET_WM_ICON)

Best of luck! Unfortunately, the code is not supported.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCS2kUJd01MZaTXX0RAoEiAJsEYXT4aNXwfmWhcS0lFN HXj3+q7QCfSK8z
jJDV5/uLWoCY5tggDUtV17A=
=5yBr
-----END PGP SIGNATURE-----

Jul 18 '05 #4
Jeff Epler wrote:
I have written a rather hackish extension to use NET_WM_ICON to set
full-color icons in Tkinter apps. You can read about it here:
http://craie.unpy.net/aether/index.c...re/01112237744
you'll probably need to take a look at the EWMH spec, too. If KDE
supports NET_WM_ICON, this may work for you (but you'll have to convert
your image manually to the format required for NET_WM_ICON)

Best of luck! Unfortunately, the code is not supported.

Jeff


Thanks very much for the link! I'll take a look.

Tim J

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Wayne Aprato | last post by:
Is there any way to have the application icon in the same folder as my database and programmatically tell the database to look for it in that location? Thanks in advance.
3
by: Sam | last post by:
I've been googling for a couple of hours now and tried some different things, but I've been unable to get the icon for a distributed access application to do anything but display the default...
3
by: Wayne | last post by:
I currently have an app whose Icon I am setting. I want to set the icons in my forms at run time to that of the application icon. How do I retrieve the application Icon so that I can use it for my...
2
by: Elad Gutman | last post by:
Hi Everyone, I'm facing an annoying problem and any help will be most appreciated . .. . I have a main application form and its icon is functioning excellent: it appears both in the taskbar...
6
by: Paul | last post by:
In other development environments i've used the IDE had facilities to create custom icons for the application and files it created. It would be nice to have a custom icon for a created file, but...
3
by: QLD_AU | last post by:
Is their a way in a VB.Net application to set all sub form icons to the main form ? or even set all icons to the Application icon, without having to reference the icon as a filename ? With...
4
by: Grumpy Aero Guy | last post by:
I've looked far and wide, but can't find any info on this. I have found the property for a FORM that allows you indicate whether, or not, the form appears in the TASK BAR... What about if I...
2
by: UJ | last post by:
How do you set the icon for when somebody presses Shift-Tab ? I have already set the application icon and the form icon but they aren't showing up in Shift-Tab. TIA - Jeff
1
by: Dick | last post by:
I have a shared project that contains icons and bitmaps that are used throughout my suite of applications. I have to set Form.Icon, PictureBox.Image, etc. using code because the form designer...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.