hii readers,
Right Click Popup Menu in Text Editor Is very important to do any work in easy way. because with this popup menu, a programmer can provide many types of useful functionality to user in mouse right click. With This Menu, we can provide many features like copy, paste, cut, select all, undo, redo and many more right under the click of mouse.
So, In this Tutorials, I will show you how to add simple right click context or popup menu in tkinter text widget.
But first,
if you are a new visitor, Then I will suggest you to read our tutorials number wise. because in these tutorial series i am showing how to use python tkinter module to create a real life application.Links For Tutorials
MagicStick Text Editor Part 3MagicStick Text Editor Part 4MagicStick Text Editor Part 5MagicStick Text Editor Part 6MagicStick Text Editor Part 7MagicStick Text Editor Part 8MagicStick Text Editor Part 9Or You Can Also Download Our Updated Example Codes Of Python MagicStick Text Editor Project For Understanding This Project More Clearly.
Check Here Our Github Repo Now, let's focus to our main topic.
To Add this menu in tkinter text widget, first we will create a simple menu object in which we will add our all options buttons like copy, cut , paste and select all. And Then, we will bind that menu object with mouse right side button click to show menu under the cursor.
Simple!
To make this script easy to understand we will create a separate empty script in "magicsticklibs/" folder and write our codes in it.
Now, let's do some practical codings.
1. PopupMenu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | ## ## ## ################################################ ## ###### Please Don't Remove Author Name ######### ## ############# Thanks ########################### ## ################################################ ## ## __author__='''
###################################################### By ######################################################
Suraj Singh surajsinghbisht054@gmail.com http://www.bitforestinfo.com/
###################################################### ''' from Graphics import Tkinter
class Popup: def __init__(self, text): self.text = text self.functions_binding_key() self.functions_configurations()
def functions_configurations(self): self.menu = Tkinter.Menu(self.text.master) self.menu.add_command(label="Copy", command=self.text.storeobj['Copy']) self.menu.add_command(label="Cut", command=self.text.storeobj['Cut']) self.menu.add_command(label="Paste", command=self.text.storeobj['Paste']) self.menu.add_separator() self.menu.add_command(label="Select All", command=self.text.storeobj['SelectAll']) self.menu.add_separator() return
def functions_binding_key(self): self.text.bind("<Button-3>",self.show_menu_)
return
def show_menu_(self, event): self.menu.tk_popup(event.x, event.y) return
if __name__ == '__main__': root = Tkinter.Tk() text = Tkinter.Text() text.pack() text.storeobj={'SelectAll':None, "Copy":None, "Cut":None, "Paste":None} Popup(text) root.mainloop()
|
To Run These Script Codes alone, Just Change line 24 from
"from Graphics import Tkinter"
To "import Tkinter"
Or
To Run This Codes as MagicStick Script. Don't Do Any Changes in this codes.
Now, let's join this script in MagicStick Text Editor Project ( Follow Below Steps only, when you have not Connected this script with MagicScript Main Stream).
To Connect This Script With MagicStick project, we need to do only two steps.
1. first, Place This Script In Our "MagicStick/magicsticklibs" Project Directory.
2. Add Function Of This Script in Our "ConfigSettings.py".
Let me explain you these steps in more details.
Step 1.
Place "PopupMenu.py" In Our "MagicStick/magicsticklibs" Project Directory.
Then Our MagicStick Project Directory Structure will look like this.
MagicStick_Editor_Part_5
|
|---> About.txt
|
|---> magicsticklibs
| |
| |---> __init__.py
| |
| |---> Main.py
| |
| |---> TextPad.py
| |
| |---> Graphics.py
| |
| |---> ConfigSettings.py
| |
| |---> ScrollBar.py
| |
| |---> LineNumber.py
| |
| |---> StationeryFunctions.py
| |
| |---> PopupMenu.py
|
|
|---> main.py
Step 2.
Add "PopupMenu.py" Function in Our "ConfigSettings.py" because in this project, "ConfigSettings.py" is responsible for joining libs in main stream.
ConfigSettings.py Modified Codes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | ## ## ## ################################################ ## ###### Please Don't Remove Author Name ######### ## ############# Thanks ########################### ## ################################################ ## ## __author__='''
###################################################### By ######################################################
Suraj Singh surajsinghbisht054@gmail.com http://www.bitforestinfo.com/
###################################################### '''
from LineNumber import LineMain from ScrollBar import Scrollbar from StationeryFunctions import StationeryFunctions from PopupMenu import Popup
class Connect: def __init__(self, pad): self.pad = pad self.modules_connections()
def modules_connections(self): LineMain(self.pad) Scrollbar(self.pad) StationeryFunctions(self.pad) Popup(self.pad) return
|
Boom! Our PopupMenu.py script is now connected with MagicStick Project. To run this script in main stream of project. just run "main.py" script or you can also run this script alone and parallelled without touching project main stream just run "PopupMenu.py".
My ScreenShot
This Tutorial Ends Here,
In My Next Tutorial, We will Continue This project.
Written By