Header Ads

Header ADS

Creating pop-up / Right click menu in tkinter


Pop-up / Right click Menu in Tkinter 

Popup Menu

Popup menus are context menus that appear on user interaction. This menu can be shown anywhere on the client window.



A general example  of pop-up menu is when you press right mouse button on computer main window screen and a menubar with options refresh , create folder is pop-up.

In this Blog we will create a Pop-up menu using Tkinter Python .

Code to create :

import tkinter 
from tkinter import *

def func():
	print('Dynamic Coding')

root = Tk() 
root.title("Dynamic Coding")

# creating menu-bar ...  
m = Menu(root, tearoff = 0) 
m.add_command(label ="Cut",command=func) 
m.add_command(label ="Copy",command=func) 
m.add_command(label ="Paste",command=func) 
m.add_command(label ="Reload",command=func) 
m.add_command(label ="Rename",command=func) 
  
def do_popup(event): 
    try: 
        m.tk_popup(event.x_root, event.y_root) 
    finally: 
        m.grab_release() 

# binding mouse right button to root 
root.bind("<Button-3>", do_popup)  
root.mainloop()


Output :




Functions :

  1. Menu(root): creates the menu.
  2. add_command(label, command): adds the commands on the menu, the command argument calls the function func( ) when that option is clicked.
  3. tk_popup(x, y): posts the menu at the position given as arguments.
  4. grab_release(): releases the event grab.
  5. bind(key, event): binds the mouse event.








This Article is Contributed by Yogesh singh .
Share your suggestions and feedback in comment sections.

Thank you for visiting here ..










No comments

Powered by Blogger.