Popup Menu in Tkinter - learnit

Home Top Ad

Post Top Ad

Friday, April 2, 2021

Popup Menu in Tkinter

Popup Menu in Tkinter

Popup Menu in Tkinter

Python

Python is a general-purpose, versatile, and powerful programming language. It is a great first language because it is concise and easy to read. Whatever you want to do, Python can do it. From web development to machine learning to data science


1)Great first language

2)Large programming community

3)Excellent online documentation

4)Endless libraries and packages

5)Worldwide popularity

6)Powerful and flexible


Popup Menu in Tkinter

Tkinter is Python’s standard GUI (Graphical User Interface) package. It is one of the most commonly used packages for GUI applications, which comes with the Python itself.


Menu Widget

Menus are an important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipulating data. Toplevel menus are displayed just under the title bar of the root or any other top-level windows.


Popup Menu

Popup menus are context menus that appear on user interaction. This menu can be shown anywhere on the client window. Below is the python code to create a popup menu using Tkinter library.


Functions

-Menu(root): creates the menu.

-add_command(label, command): adds the commands on the menu, the command argument calls the function hey() when that option is clicked.

-add_separator(): adds a separator.

-tk_popup(x, y): posts the menu at the position given as arguments

-bind(key, event): binds the mouse event

-grab_release(): releases the event grab


Syntax

Menu= Menu (master, **options)


Example

from tkinter import *
from tkinter import ttk
root=Tk()
root.title('reanits.com')
root.iconbitmap('1.png')
root.geometry("300x400")
mylabel= Label(root,text="",font=("Calibri",12))
mylabel.pack(pady=20)
def cambodia():
    mylabel.config(text="Knowledge Edcucation Cambodia")
def reanits():
    mylabel.config(text="reanits.com")
def mypopup(e):
    mymenu.tk_popup(e.x_root, e.y_root)
mymenu= Menu(root, tearoff=False)
mymenu.add_command(label="Knowledge Edcuation Cambodia",command=cambodia)
mymenu.add_command(label="reanits.com",command=reanits)
mymenu.add_separator()
mymenu.add_command(label="Goodbye",command=root.quit)
root.bind("<Button-3>",mypopup)
root.mainloop()

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad