Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Tkinter GUI Application Development Blueprints

You're reading from  Tkinter GUI Application Development Blueprints

Product type Book
Published in Nov 2015
Publisher Packt
ISBN-13 9781785889738
Pages 340 pages
Edition 1st Edition
Languages
Author (1):
Bhaskar Chaudhary Bhaskar Chaudhary
Profile icon Bhaskar Chaudhary

Table of Contents (15) Chapters

Tkinter GUI Application Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Meet Tkinter 2. Making a Text Editor 3. Programmable Drum Machine 4. A Game of Chess 5. Building an Audio Player 6. Paint Application 7. Multiple Fun Projects 8. Miscellaneous Tips Index

Creating the context/pop-up menu


Let's complete the editor in this final iteration by adding a contextual menu to the editor (refer to 2.12.py in the code bundle), as shown in the following screenshot:

The menu that pops up on the right-mouse-button click at the location of the mouse cursor is called the context menu or the pop-up menu.

Let's code this feature in the text editor. First define the context menu, as follows:

popup_menu = Menu(content_text)
for i in ('cut', 'copy', 'paste', 'undo', 'redo'):
    cmd = eval(i)
    popup_menu.add_command(label=i, compound='left', command=cmd)
popup_menu.add_separator()
popup_menu.add_command(label='Select All', underline=7, command=select_all)

Then, bind the right-click of a mouse with a callback named show_popup_menu, as follows:

content_text.bind('<Button-3>', show_popup_menu)

Finally, define the show_popup_menu function in the following way:

def show_popup_menu(event):
    popup_menu.tk_popup(event.x_root, event.y_root)

You can now right-click...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime}