0
votes

I am trying to make window with custom title bar in tkinter python.
I have added close button and minimize button in the window.
Here is the code:

class custom_titlebar(tk.Frame):
    def __init__(self,master= None,*args,**kwargs):
        tk.Frame.__init__(self,master = None,*args,**kwargs)
        self.pclose = ImageTk.PhotoImage(Image.open("Close.png").resize((27,24),Image.ANTIALIAS))
        self.pminimize = ImageTk.PhotoImage(Image.open("Minimize.png").resize((27,24),Image.ANTIALIAS))
    def create_basics(self):
        self.up = tk.Frame(self,width = 750,height = 10,bg = "#cdb990")
        self.close = tk.Button(self.up,image = self.pclose,borderwidth = 0,bg = "#cdb990")
        self.close.pack(side= RIGHT,padx = 7,pady = 7)
        self.close.bind("<Button-1>",lambda event:self.master.destroy())
        self.minimize_button = tk.Button(self.up,image = self.pminimize,borderwidth = 0,bg = "#cdb990")
        self.minimize_button.pack(side= RIGHT,pady = 7)
        self.minimize_button.bind("<Button-1>",lambda event:self.minimize_window(event))
        self.up.pack(fill = X)
    def minimize_window(self,event):
        self.master.overrideredirect(0)
        self.master.wm_state("iconic")

if __name__ == "__main__":
    window = tk.Tk()
    window.overrideredirect(1)
    ex = custom_titlebar(window,height = 750,width = 750)
    ex.create_basics()
    ex.pack()
    window.mainloop()  

The problem is whenever I minimize the window and open it again from the taskbar then I want the title bar to be removed again as before.
How can I implement this in my code.
Thanks in advance for any help.

1

1 Answers

1
votes

You can bind on "<Map>" event and call overrideredirect(1) when custom_titlebar is viewable.

Below is an example:

class custom_titlebar(tk.Frame):
    def __init__(self, master=None, *args, **kwargs):
        ...
        self.bind("<Map>", self.on_map)
    ...
    def on_map(self, event):
        self.master.overrideredirect(1)