how to add copy,cut,paste feature in python tkinter text widget - Python MagicStick Text Editor - Part 5

Posted by Suraj Singh on May 14, 2017 · 16 mins read
hii readers,




In Text Editor, copy, cut and paste options always help user to copy or cut any specific line, character or word. With Copy, Cut and Paste Options, we don't need to type any specific word or line again and again. Hence, These features is very important for our text editor.

So, In this Post, i am going to show you how to add copy,cut,paste feature in python tkinter text widget or you can say how to add copy,cut and paste features in Tkinter text widgets.


This Is our Fifth part of Python MagicStick Text Editor Project and In this post, i will show you how to add Copy, Cut and Paste feature easily in MagicStick python project.

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 Introduction
MagicStick Text Editor Part 1
MagicStick Text Editor Part 2
MagicStick Text Editor Part 3
MagicStick Text Editor Part 4
MagicStick Text Editor Part 5
MagicStick Text Editor Part 6
MagicStick Text Editor Part 7
MagicStick Text Editor Part 8
MagicStick Text Editor Part 9
MagicStick Text Editor Part 10
MagicStick Text Editor Part 11
MagicStick Text Editor Part 12
MagicStick Text Editor Last Part

Or 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.

Here, For Handling This Types Of Feature we will create a separate file called  "StationetyFunctions.py". This Script Will Works like a special script for handling Stationery work.


Check Below my Example Codes.



1. "StationeryFunctions.py"



 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
##
##
## ################################################
## ###### Please Don't Remove Author Name #########
## ############# Thanks ###########################
## ################################################
##
##
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''
from Graphics import Tkinter

class StationeryFunctions:
def __init__(self, text):
self.text = text
self.create_binding_keys()
self.binding_functions_config()
self.join_function_with_main_stream()


def join_function_with_main_stream(self):
self.text.storeobj['Copy'] = self.copy
self.text.storeobj['Cut'] = self.cut
self.text.storeobj['Paste'] = self.paste
self.text.storeobj['SelectAll']=self.select_all
self.text.storeobj['DeselectAll']=self.deselect_all
return

def binding_functions_config(self):
self.text.tag_configure("sel", background="skyblue")
return

def copy(self, event):
self.text.event_generate("<<Copy>>")
return

def paste(self, event):
self.text.event_generate("<<Paste>>")
return

def cut(self, event):
self.text.event_generate("<<Cut>>")
return

def create_binding_keys(self):
for key in ["<Control-a>","<Control-A>"]:
self.text.master.bind(key, self.select_all)
for key in ["<Button-1>","<Return>"]:
self.text.master.bind(key, self.deselect_all)
return

def select_all(self, event):
self.text.tag_add("sel",'1.0','end')
return


def deselect_all(self, event):
self.text.tag_remove("sel",'1.0','end')
return

if __name__ == '__main__':
root = Tkinter.Tk()
pad = Tkinter.Text(root,wrap='none')
pad.storeobj = {}
StationeryFunctions(pad)
pad.pack()
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.

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 "StationeryFunctions.py" In Our "MagicStick/magicsticklibs" Project Directory.


Them Our MagicStick Project Directory Structure will look like this.



 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
MagicStick_Editor_Part_4
|
|---> About.txt
|
|---> magicsticklibs
| |
| |---> __init__.py
| |
| |---> Main.py
| |
| |---> TextPad.py
| |
| |---> Graphics.py
| |
| |---> ConfigSettings.py
| |
| |---> ScrollBar.py
| |
| |---> LineNumber.py
| |
| |---> StationeryFunctions.py
|
|
|---> main.py


Step 2.
          Add "StationeyFunctions.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
##
##
## ################################################
## ###### 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

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)
return


Boom! 

Boom! Our StationeyFunctions.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 ".py".
This Tutorial Ends Here,


In My Next Tutorial, We will Continue This project.


Written By