1
votes

I have a tkinter window that I removed the title bar from and added a custom close and minimize button to. When the program first loads it doesn't display an icon to the taskbar. When I click the custom made minimize button it then creates an icon on the taskbar; however, when I click to restore the window I am unable to get rid of the titlebar again.

I want the icon to always show on the taskbar, and when the program is minimized and then restored I would like the title bar to still be gone from .overrideredirect(1). Unfortunately I'm having trouble resetting the flag before and after minimizing without making the taskbar icon disappear.

Please let me know what I am doing wrong. Thanks!

#!/usr/bin/python3
from tkinter import *
import tkinter as tk
import datetime
import time
import math

root = tk.Tk() 

root.overrideredirect(1)

def close():
    root.destroy()
def minimizeWindow():
    root.withdraw()
    root.overrideredirect(False)
    root.iconify()

root.resizable(False, False)
canvas = Canvas(root, width = 400, height = 400)
canvas.pack()
exit = Button(root, text='x', command = close)
exitWindow = canvas.create_window(10,10, window=exit)
minimize = Button(root, text='-', command = minimizeWindow)
minimizeWindow = canvas.create_window(30,10,window=minimize)
icon = PhotoImage(file='py.gif')
root.tk.call('wm', 'iconphoto', root._w, icon)
root.mainloop() # starts the mainloop

I am trying to make it work for both windows and linux. The reason I'm taking out the title bar altogether is to avoid the differences that come from the OS font and window settings. It's currently exhibiting this same behavior on both Operating Systems.

To reiterate, I want the taskbar icon to show up on program launch and I want the program's window to maintain it's titlebar-less state when restored from being minimized.

2
What are you doing with the icon? It does not work here at all. Are you trying to keep it and use it in your own bar?Mike - SMT
The icon seems to work in Linux but not in Windows. I'm more concerned with it displaying an icon at all on the taskbar when it is first started, as well as the overrideredirect. When it's minimized and restored I don't want the title bar to show up.Jamin
On windows I can create an label holding an icon on the canvas. That should work for you here. Is there a reason to use canvas over a frame here?Mike - SMT
Yes because I'm going to use the canvas class to draw somethingJamin
You can still use canvas to draw something you do not need to use it for the bar. Unless you are wanting to draw something on the bar? If you do want to draw on the BAR you can still do this with a canvas and just place the canvas object in the bar frame.Mike - SMT

2 Answers

2
votes

It depends on what operating system you are using. If you are using Windows the below solution should work for you.

I have added a function that will reapply the overriderdirect. This function is being called by a bind we used on root.

I have also changed your canvas to a frame as this make it easier to manage things like buttons.

For linux you may need to use a different file type. On window you use .ico and on linux you may need to use .xbm.

See this answer about it on this post: Python 3 tkinter iconbitmap error in ubuntu

Update:

I have added the iconbitmap and root.tk.call('wm', 'iconphoto', root._w, icon) however I am not sure if you will be able to make your taskbar icon change until you compile the code at least in windows. You can use py2exe or freeze. I have used freeze before and I have a customer desktop and taskbar icon I use for it.

import tkinter as tk

root = tk.Tk() 
root.geometry("400x400")
root.overrideredirect(1)
root.resizable(False, False)
root.columnconfigure(0, weight=1)
root.iconbitmap(default='./Colors/small_red.ico')


def close():
    root.destroy()

def minimizeWindow():
    root.withdraw()
    root.overrideredirect(False)
    root.iconify()

def check_map(event): # apply override on deiconify.
    if str(event) == "<Map event>":
        root.overrideredirect(1)
        print ('Deiconified', event)
    else:
        print ('Iconified', event)

bar_frame = tk.Frame(root)
bar_frame.grid(row=0, column=0, sticky="ew")
bar_frame.columnconfigure(0, weight=1)
icon = tk.PhotoImage(file='./Colors/small_red.gif')
# This appears to have the same results so not sure what the difference is from iconbitmap.
# root.tk.call('wm', 'iconphoto', root._w, icon) 

tk.Button(bar_frame, text='x', command=close).grid(row=0, column=1)
tk.Button(bar_frame, text='-', command=minimizeWindow).grid(row=0, column=2)

root.bind('<Map>', check_map) # added bindings to pass windows status to function
root.bind('<Unmap>', check_map)

root.mainloop()
0
votes

There Are Two Ways

If you want to restore it in a new/custom Taskbar Just do this:-

from tkinter import *
import tkinter as tk
import datetime
import time
import math

root = tk.Tk() 

def close():
    root.destroy()
def minimizeWindow():
    root.update_idletasks()
    root.overrideredirect(False)
    root.state('iconic')
    root.attributes('-topmost', True)
    root.overrideredirect(True)
    root.geometry("215x330")
root.wm_overrideredirect(True)
root.attributes('-topmost', False)
root.resizable(False, False)
canvas = Canvas(root, width = 400, height = 400)
canvas.pack()
exit = Button(root, text='x', command = close)
exitWindow = canvas.create_window(10,10, window=exit)
minimize = Button(root, text='-', command = minimizeWindow)
minimizeWindow = canvas.create_window(30,10,window=minimize)
icon = PhotoImage(file='py.gif')
root.tk.call('wm', 'iconphoto', root._w, icon)
root.mainloop()

Else if you want to restore in your windows Taskbar:-

from tkinter import *
import tkinter as tk
import datetime
import time
import math

root = tk.Tk() 

root.overrideredirect(1)
def check(event):
    if str(event) == "<Map event>":
        window.overrideredirect(1)
    else:
        None
def close():
    root.destroy()
def minimizeWindow():
    root.withdraw()
    root.overrideredirect(False)
    root.iconify()
root.overrideredirect(True)

root.resizable(False, False)
canvas = Canvas(root, width = 400, height = 400)
canvas.pack()
exit = Button(root, text='x', command = close)
exitWindow = canvas.create_window(10,10, window=exit)
minimize = Button(root, text='-', command = minimizeWindow)
minimizeWindow = canvas.create_window(30,10,window=minimize)
icon = PhotoImage(file='py.gif')
root.tk.call('wm', 'iconphoto', root._w, icon)
root.bind('<Map>', check_map) 
root.bind('<Unmap>', check_map)
root.mainloop() # starts the mainloop