Removing the TK icon on a Tkinter window
Asked Answered
S

8

28

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

Sanitarium answered 15/2, 2009 at 0:27 Comment(0)
B
47

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.

Brentonbrentt answered 16/4, 2009 at 4:11 Comment(3)
Rather than creating an icon, you can just Google "blank icon" and download one. For instance: iconspedia.com/icon/blank-icon-44576.htmlSternick
@Sternick Yes, that's an option too, but then you have to comply with the licence details of whichever designer created the icon. They shouldn't be too bad, though, so it's a good idea.Brentonbrentt
The best solution is actually Dario's answer below where you can embed the transparent icon into the code, and thus not have to have an extra file in your codebase.Brentonbrentt
F
16

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='))
Fatima answered 16/8, 2013 at 15:48 Comment(4)
how did you get the 'ICON = (...' in that format? I'm struggling... #29508390Tullis
It was a pain to do. I used the rw-designer that @Brentonbrentt mentioned in his answer and generated a transparent .ico file. I then opened up the binary file and copy/pasted the data into my python program. I noticed there was a lot of repetition, so I shortened it some - hence the b'\x00'*1282 + b'\xff'*64. It might be possible to simplify further if you look up the .ico format specs, but that was good enough for what I was doing.Fatima
You can use the BitmapImage or PhotoImage classes to convert the string without creating a temporary file.Iorgos
@PeterWood I get _tkinter.TclError: bitmap "pyimage1" not defined when I run tk.iconbitmap(default=ICON_PATH) using the BitmapImage class and _tkinter.TclError: couldn't recognize image data when I try creating the PhotoImage object.Fatima
H
4

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()
Han answered 1/6, 2016 at 10:47 Comment(2)
The benefit of this method is that it's done in memory, no need for HD writesZoa
this just replaces the default tk leaf icon with a black square - tested on 3.9.12Subarid
C
3

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

Coign answered 15/2, 2009 at 0:38 Comment(0)
D
2

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

Dervish answered 14/10, 2018 at 15:2 Comment(0)
R
1

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?

Revelation answered 8/4, 2017 at 17:39 Comment(1)
Updated questions should be posted as new questions, with a link to the old one. Questions posted in answers will often not be noticed for a long time...Brentonbrentt
L
1

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 !

Lemmon answered 1/3, 2021 at 14:29 Comment(0)
G
-1

To remove the feather icon from the title bar of a tkinter window, you can use the wm_attributes method with the type argument set to 'toolwindow'. Here’s an example code snippet that demonstrates how to remove the feather icon on Windows:


import tkinter as tk

root = tk.Tk()

root.wm_attributes('-toolwindow', True)

root.mainloop()


This will create a tkinter window without the feather icon on the title bar. Note that this solution only works on Windows.

Note: This answer is AI generated. It is tested, it works on Python 3.11

Grasso answered 8/1 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.