typedecker
typedecker

Reputation: 1389

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

Upvotes: 3

Views: 1056

Answers (1)

Thingamabobs
Thingamabobs

Reputation: 8082

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.

Upvotes: 1

Related Questions