Popup menus in Tkinter

Hans Nowak ivnowa at hvision.nl
Mon Nov 8 18:27:27 CET 1999


On 8 Nov 99, dkaznadzey at my-deja.com wrote:

> Does anyone know weather there is a way to create
> a popup menu in tk (using Tkinter)? It seems
> like in order to be shown the menu must
> grow from the valid menubar. If there is a way
> to display standalone menu pane at given
> coordinates, please let me know.

Try this code... You can right-click on the canvas to make the menu 
pop up.

# menutest.py
# Makes a window pop up by rightclicking the canvas.

from Tkinter import *
import regsub

def getgeometry(something):
    s = something.geometry()
    return map(int, regsub.split(s, "[x+]"))

root = Tk()

root.canvas = Canvas(root, height=100, width=100,   
background='#674689')
root.canvas.pack()

def new_file(event=None):
    print "Opening new file"

menu = Menu(root)
menu.add_command(label="New...", underline=0, command=new_file)
menu.add_command(label="Open...", underline=0, command=new_file)
menu['tearoff'] = 0

def activate_menu(event=None):
    sizex, sizey, x, y = getgeometry(root)    
    menu.tk_popup(x+event.x, y+event.y)

root.canvas.bind("<Button-3>", activate_menu)

root.mainloop()

--Hans Nowak (zephyrfalcon at hvision.nl)
Homepage: http://fly.to/zephyrfalcon
You call me a masterless man. You are wrong. I am my own master.




More information about the Python-list mailing list