Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Title bar menu in tkinter

I think most of us must have seen the command prompt(windows) and how when we open it and click on it's icon and it shows a menu. Can we do a similar thing with tkinter? This is not the normal menubar.

Here is an illustration of the command prompt one.

Command Prompt Icon/Title Bar Menu

like image 850
typedecker Avatar asked Feb 05 '26 06:02

typedecker


1 Answers

This is just an exampel of a work around without the need of doing all your window management by your own. Of course it will need improvements but as a start: Popup stolen from

import tkinter as tk
from PIL import Image, ImageTk

def popup(event):
    popup_menu.tk_popup(event.x_root, event.y_root, 0)

def set_icon():
    global top, popup
    top = tk.Toplevel(root)
    top.overrideredirect(1)
    top.attributes('-topmost',True)
    offset = 30
    x,y = root.winfo_rootx(),root.winfo_rooty()-offset
    width, height = offset,offset
    top.geometry("%dx%d+%d+%d" % (width,height, x,y))
    my_label = tk.Label(top, image=photo)
    my_label.pack(fill='both')
    global popup_menu
    popup_menu = tk.Menu(top, tearoff=0)
    popup_menu.add_command(label="Delete",
                           command=lambda :print('del'))
    popup_menu.add_command(label="Select All",
                           command=lambda :print('sel'))

    top.bind("<Button-1>", popup)

def grab(event):
    top.geometry(f'+{event.x+10}+{event.y+2}')

root=tk.Tk()

ico = Image.open('prac_img/p2.png')
photo = ImageTk.PhotoImage(ico)
root.iconphoto(False,photo)
#root.wm_iconphoto(False,photo)
root.bind('<Configure>',grab)
root.update_idletasks()
set_icon()

root.mainloop()

Another way would be to code your own titlebar and the use of overrideredirecr(1) which will undecorate your window by the window manager of your system.

like image 59
Thingamabobs Avatar answered Feb 06 '26 18:02

Thingamabobs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!