Evan Fosmark
Evan Fosmark

Reputation: 101661

Removing the TK icon on a Tkinter window

How to remove tkinter icon from title bar in it's window

Upvotes: 28

Views: 53290

Answers (7)

HeapHeapHooray
HeapHeapHooray

Reputation: 11

Recently, I've found a solution for Linux in an old Ubuntu forum's post.

The solution makes use of iconbitmap and iconmask with a blank (filled with zeroes) .xbm file.

So, first of all, what is a .xbm file?

A .xbm file is a plain text (it's actually C code !) image format commonly used for storing bitmaps, the data is stored as a 2D Matrix (a two-dimensional array, or an array of arrays) with 8-bits (256 possible values, from 0 to 255) sized elements, the Matrix is represented as a single one-dimensional array plus values that define it's shape.

The data can be interpreted in any way, but it is usually interpreted as a monochrome/grayscale image.

iconbitmap

Our first method is called iconbitmap and is used to set a bitmap as the icon of our tkinter window, we can input the location of a .xbm file and it will be represented as a monochrome/grayscale icon, where the pixels/elements of the .xbm represent different grayscale levels, remember the pixels/elements are 8-bits values in the range 0-255, so the value 0 will be represented as black and 255 will be represented as white and the other values are going to be interpolations between the two values.

iconmask

Our second method is called iconmask and here is where the magic happens, it is used to set alpha/transparency values to the pixels of our window's icon, we can input the location of a .xbm file and elements with value 0 in the .xbm will be set as fully transparent, I've tested it on Debian with xfce4 and the transparency values don't seem to interpolate linearly in the range 0-255.

Our Solution

With that information we can create a transparent icon with tkinter in the following way:

import tkinter

tk = tkinter.Tk()

# Your file path has to start with an "@" otherwise the methods will raise an exception.
xbm_location = "@blank.xbm"
# Here we call the iconbitmap method with the xbm location, since it's a blank xbm (filled with zeroes) it will be represented as a black icon.
tk.iconbitmap(xbm_location)
# Here we call the iconmask method with xbm location, since it's a blank xbm (filled with zeroes) it will be represented as a fully transparent icon.
tk.iconmask(xbm_location)

And there is our solution, I've also tested it on Windows and the system seems to just ignore the iconmask method, so stick with the other solutions for your Windows code.

Another possible solution is using the method iconphoto with a PhotoImage loaded from a fully transparent .gif file or even create a blank PhotoImage as suggested by Jay, but it also doesn't seem to work properly on Windows.

How to create a blank .xbm?

It's pretty easy to create a blank .xbm, just download a .xbm file, create from scratch, or convert an image to .xbm and if the .xbm is not blank (filled with zeroes) already, open the .xbm in a text editor and replace all the values inside the array with "0x00".

Conclusion

That's it, A blank .xbm, iconbitmap and iconmask. Now you should be able to create fully transparent icons in a tkinter window on Linux...

Hooray !

Upvotes: 1

shbooms
shbooms

Reputation: 93

Alternative to @ubomb's solution for adding custom images by utilizing Tkinter.PhotoImage's built-in support for processing .gif images.

From file:

icon = Tkinter.PhotoImage(file="logo.gif")

from base64:

gif_base64_string = """ R0lGODdhEAAQAIcAAAAAAAEBAQICAgMDAwQEBAUFBQYGBgcHBwgICAkJCQoKCgsLCwwMDA0NDQ4O Dg8PDxAQEBERERISEhMTExQUFBUVFRYWFhcXFxgYGBkZGRoaGhsbGxwcHB0dHR4eHh8fHyAgICEh ... 4B8AAP9Ci/4HoLTpfwD+qV4NoHVAADs= """

icon = Tkinter.PhotoImage(data=gif_base64_string)

Visit the undermentioned link for more details: //effbot.org/tkinterbook/photoimage.htm

Upvotes: 2

Jay
Jay

Reputation: 2858

Update:

The slightly modified code (try clause instead of if TkVersion) produces a transparent (no) icon on:

Linux (Mint 18.1), Python 2.7

Linux (Mint 18.1), Python 3.5.1

Windows 10, Python 2.7.13

It produces a black icon (does not work) on:

Windows 8.1, Python 3.6

A rather old question, but the solutions weren't working for me. I found a partial simple solution, with a follow-up question of my own.

The partial solution (Tk 8.5, see below) - using PhotoImage's blank() method:

from Tkinter import *

root=Tk()

icon=PhotoImage(height=16, width=16)
icon.blank()

root.tk.call('wm', 'iconphoto', self.master._w, icon)

root.mainloop()

On Python 2.7, Windows 10, this works fine, producing the desired "no icon" for your new app.

However, on Python 3.6, Win 8.1, this jams the GUI, which I think is related to the newer Tk 8.6, and though I found that the new 8.6 notation of using wm_iconphoto() does pass unjammed in this case:

try:
    from tkinter import *
except:
    from Tkinter import * 

root=Tk()

#Identical for Py2.7/Tk8.5 and Py3.5/Tk8.6
icon=PhotoImage(height=16, width=16)
icon.blank()

#Picking a notaion based on Tk version to avoid jamming
try:
    root.wm_iconphoto('True', icon)   #New Tk 8.6 style        
else:
    #Jams Python 3.5 with Tk 8.6 on Windows
    root.tk.call('wm', 'iconphoto', self.master._w, icon)   



root.mainloop()

It produces a black icon on 3.6, instead of the transparent one in case of 2.7/8.5.

There might be a way to set the pixels transparent one by one using "transparency set" - http://wiki.tcl.tk/1449

However, I don't know if it's even doable via Tkinter. God favors the bold, someone else's turn though?

Updated question: Why doesn't this work on Py3.6/Windows?

Upvotes: 1

Darío M.
Darío M.

Reputation: 141

Based on previous responses i used this solution:

from PIL import ImageTk
import zlib,base64
import Tkinter

icon=zlib.decompress(base64.b64decode('eJxjYGAEQgEBBiDJwZDBy'
'sAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4JRMApGwQgF/ykEAFXxQRc='))
root=Tkinter.Tk()
image=ImageTk.PhotoImage(data=icon)
root.tk.call('wm', 'iconphoto', root._w, image)
root.mainloop()

Upvotes: 4

ubomb
ubomb

Reputation: 11571

Similar to the accepted answer (with the con of being uglier):

import tkinter
import tempfile

ICON = (b'\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00\x08\x00h\x05\x00\x00'
        b'\x16\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00 \x00\x00\x00\x01\x00'
        b'\x08\x00\x00\x00\x00\x00@\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        b'\x00\x01\x00\x00\x00\x01') + b'\x00'*1282 + b'\xff'*64

_, ICON_PATH = tempfile.mkstemp()
with open(ICON_PATH, 'wb') as icon_file:
    icon_file.write(ICON)

tk = tkinter.Tk()
tk.iconbitmap(default=ICON_PATH)
label = tkinter.Label(tk, text="Window with transparent icon.")
label.pack()

tk.mainloop()

It just creates the file on the fly instead, so you don't have to carry an extra file around. Using the same method, you could also do an '.xbm' icon for Unix.

Edit: The ICON can be shortened even further thanks to @Magnus Hoff:

import base64, zlib

ICON = zlib.decompress(base64.b64decode('eJxjYGAEQgEBBiDJwZDBy'
    'sAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4JRMApGwQgF/ykEAFXxQRc='))

Upvotes: 16

Stobor
Stobor

Reputation: 45122

On Windows

Step One:

Create a transparent icon using either an icon editor, or a site like rw-designer. Save it as transparent.ico.

Step Two:

from tkinter import *

tk = Tk()
tk.iconbitmap(default='transparent.ico')
lab = Label(tk, text='Window with transparent icon.')
lab.pack()
tk.mainloop()

On Unix

Something similar, but using an xbm icon.

Upvotes: 47

user36457
user36457

Reputation:

As far as I know, the closest you will get to a "blank" icon is using one that's the same color as the window title bar. But then again a lot of users use different color themes, so it won't go over very well.

However if you use py2exe you can use something like Resource Hacker to swap the icon. But in the python programs text state, the best you can do is replace. Sort of how Jar files use the java icon, tkinter apps will have the TK icon. After all...like java, your app is being translated by an intermediate program. Since another program is running your code, you have to modify that other program. Luckily python/tk is a bit more flexible than the JVM in terms of icons so you can replace the icon. But removing it entirely isn't currently an option.

-John

Upvotes: 3

Related Questions