Search code examples
python-3.xtkinterwindows-10iconsguizero

How do I add back the icon to the task bar in guizero/tkinter python 3?


I am trying to add back the icon for a custom app window with the border removed. I made this in Guizero/Tkinter. I Need to be able to add back the icon so it can be displayed in the taskbar. The icon need to be displayed as active so that you can hide/minimize into the task bar and show/reopen the window from there. I have tried "root.iconify()".

from guizero import *

app=App(title='Test',bg='#121212',width=750,height=550)
root = app.tk
#Icon does not display when overrideredirect is True
app.tk.iconbitmap("icon.ico")


#Remove window border
root.overrideredirect(True)
#https://stackoverflow.com/questions/14910858/how-to-specify-where-a-tkinter-window-opens
#base on answer given byRachel Gallen
def center_window(width=750, height=500):
    # get screen width and height
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # calculate position x and y coordinates
    x = (screen_width/2) - (width/2)
    y = (screen_height/2) - (height/2)
    root.geometry('%dx%d+%d+%d' % (width, height, x, y))


#menu bar
Top_box = Box(app,height=35,width='fill', align="top")
Top_box.bg='white'

#window title
left_box = Box(Top_box,height=35,width='fill', align="left")
left_box.bg='#121212'
text = Text(left_box, text=" Test Project",align="left")
text.text_color='gray'
text.tk.padx=15
text.tk.config(font='Helvetica 15 bold')

#end of window title


#Exit/quit button
Quit_button = PushButton(Top_box, text="X", command=quit ,align='right')
Quit_button.bd=0
Quit_button.text_size=22
Quit_button.text_color='purple'
Quit_button.bg='gray'
Quit_button.tk.config(activebackground='black',highlightthickness=10,bd=0,highlightbackground='red')


#Minmize/hide button
Minmize_button = PushButton(Top_box, text="-", command=app.hide ,align='right')
Minmize_button.tk.config(activebackground='green',highlightthickness=2,bd=0,highlightbackground='red',font=("Verdana", 31))
Minmize_button.text_color="purple"


#Content of the window
Canvas_box = Box(app,height='fill',width='fill', align="top")
Canvas_box.bg='green'
Text=Text(Canvas_box,text='Test',align='top',size=26)
Text.text_color='white'

center_window(750, 500)

app.display()

Solution

  • While not perfect this is pretty close. It adds the icon back when you minimize the window. It's a complete custom window with drag support. I integrated and update some code made by others to work with Guizero 1.2.0.

    from guizero import *
    app = App(bg='white',title='Test',width=645,height=275)
    #Removes window border and title bar
    app.tk.overrideredirect(True)
    #app.icon="Shorcut_Icon1.ico"
    
    #minimize system to re-add icon 
    #https://stackoverflow.com/questions/52714026/python-tkinter-restore-window-without-title-bar
    #base on Answer given by Mike - SMT
    def close():
        app.destroy()
    
    def minimizeWindow():
        app.hide()
        app.tk.overrideredirect(False)
        app.tk.iconify()
    
    def check_map(event): # apply override on deiconify.
        if str(event) == "<Map event>":
            app.tk.overrideredirect(1)
            print ('Deiconified', event)
        else:
            print ('Iconified', event)
    
    
    winControl_container=Box(app,width=app.width,height=35,align='top')
    Bottom_line=Box(winControl_container,width=app.width,height=1,align='bottom')
    Bottom_line.bg='blue'
    closeButton= PushButton(winControl_container, text='X', command=close,align='right')
    minButton= PushButton(winControl_container, text='—', command=minimizeWindow,align='right')
    
    
    #Start of window movement system
    #https://stackoverflow.com/questions/4055267/tkinter-mouse-drag-a-window-without-borders-eg-overridedirect1
    #base on answer given by David58
    lastClickX = 0
    lastClickY = 0
    
    
    def SaveLastClickPos(event):
        global lastClickX, lastClickY
        lastClickX = event.x
        lastClickY = event.y
    
    
    def Dragging(event):
        x, y = event.x - lastClickX + app.tk.winfo_x(), event.y - lastClickY + app.tk.winfo_y()
        app.tk.geometry("+%s+%s" % (x , y))
        
        
    
        
    #Adds window movement/dragging events. Just comment this out if you don't want the window to be able to move.
    app.when_left_button_pressed=SaveLastClickPos
    app.when_mouse_dragged=Dragging
    #End of movement/drag system
    
    
    app.tk.bind('<Map>', check_map) # added bindings to pass windows status to function
    app.tk.bind('<Unmap>', check_map)
    
    #Center window system
    #https://stackoverflow.com/questions/14910858/how-to-specify-where-a-tkinter-window-opens
    #base on answer given byRachel Gallen
    
    def center_window(width, height):
        # get screen width and height
        screen_width = app.tk.winfo_screenwidth()
        screen_height = app.tk.winfo_screenheight()
    
        # calculate position x and y coordinates
        x = (screen_width/2) - (width/2)
        y = (screen_height/2) - (height/2)
        app.tk.geometry('%dx%d+%d+%d' % (width, height, x, y))
    
    center_window(app.width,app.height)
    
    
    
    app.display()