how to add select_all, undo, redo feature in python tkinter text widget- Python MagicStick Text Editor - Part 6

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




In any text editor, select_all, undo, redo are very important features to help user in many different ways.

With Select All Feature we can select all text in one times, Undo is for doing our previous step back and Redo is for doing our previous undo back.

Simple, Easy and Fast.

So, In this Post, i am going to show you how to add Select all, Undo, Redo  feature in python tkinter text widget or you can say how to create a function in Tkinter text widgets to do Select all, Undo, Redo.


This Is our Sixth part of Python MagicStick Text Editor 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.

As In Previous Tutorials, We created "StationeryFunctions.py" Script For Handling Copy, Cut and Paste. Today, We will modify "StationeryFunctions.py" Script for Handling Undo, Redo and Select all functions.


Here, I am Sharing My Modified Script For Doing Undo, Redo and Select All Functions.

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
81
82
83
84
85
86
87
88
89
90
91
92
##
##
## ################################################
## ###### 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['Undo'] = self.undo
self.text.storeobj['Redo'] = self.redo
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")
self.text.configure(undo=True,autoseparators=True, maxundo=-1)
return

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

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

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

def undo(self, event=None):
self.text.event_generate("<<Undo>>")
return

def redo(self, event=None):
self.text.event_generate("<<Redo>>")
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=None):
self.text.tag_add("sel",'1.0','end')
return


def deselect_all(self, event=None):
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 ( 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 "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_5
|
|---> 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