How To display line numbers in tkinter text widget - Python MagicStick Text Editor - Part 4

Namaste Friends,




In Text Editor, line number always help user to find any specific or for remembering any line. ANd For Programmers, line numbers are like a golden way to detect any snxtax error in their code because during the execution of codes, mostly all programming language raise line number to highlight any syntax error In codes. Hence, Line Numbers are very important features for programmers and for others also.
So, In this Post, i am going to show you how to add line number in tkinter text widgets or you can say how to add line numbers showing features in Tkinter text widgets.


This Is our Fourth part of Python MagicStick Text Editor Project and In this post, i will show you how to add linenumber 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 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
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, First We will create a separated script "LineNumber.py". And In this Script, We will Create a Class That can Add Line Numbers In Text Widget.


Check Below my Example Codes.


1. "LineNumber.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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#
#
##################################################
######## Please Don't Remove Author Name #########
############### Thanks ###########################
##################################################
#
#
__author__='''

######################################################
                By S.S.B Group                          
######################################################

    Suraj Singh
    Admin
    S.S.B Group
    surajsinghbisht054@gmail.com
    http://bitforestinfo.blogspot.com/

    Note: We Feel Proud To Be Indian
######################################################
'''
from Graphics import Tkinter as tk 


class LineNumberCanvas(tk.Canvas):
    def __init__(self, *args, **kwargs):
        tk.Canvas.__init__(self, *args, **kwargs)
        self.text_widget = None
        self.breakpoints = []

    def connect(self,text_widget):
        self.text_widget = text_widget

    def re_render(self):
        """Re-render the line canvas"""
        self.delete('all') # To prevent drawing over the previous canvas

        temp = self.text_widget.index("@0,0")
        while True :
            dline= self.text_widget.dlineinfo(temp)
            if dline is None: 
                break
            y = dline[1]
            x = dline[0]
            linenum = str(temp).split(".")[0]

            id = self.create_text(2,y,anchor="nw", text=linenum)

            if int(linenum) in self.breakpoints:                
                x1,y1,x2,y2 = self.bbox(id)
                self.create_oval(x1,y1,x2,y2,fill='red')
                self.tag_raise(id)

            temp = self.text_widget.index("%s+1line" % temp)

    def get_breakpoint_number(self,event):
         if self.find_withtag('current'):
            i = self.find_withtag('current')[0]
            linenum = int(self.itemcget(i,'text'))

            if linenum in self.breakpoints:
                self.breakpoints.remove(linenum)
            else:
                self.breakpoints.append(linenum)
            self.re_render()
            
            

class LineMain:
    def __init__(self, text):
        self.text = text
        self.master = text.master
        self.mechanise()
        self._set_()
        self.binding_keys()

    def mechanise(self):
        self.text.tk.eval('''
            proc widget_interceptor {widget command args} {

                set orig_call [uplevel [linsert $args 0 $command]]

              if {
                    ([lindex $args 0] == "insert") ||
                    ([lindex $args 0] == "delete") ||
                    ([lindex $args 0] == "replace") ||
                    ([lrange $args 0 2] == {mark set insert}) || 
                    ([lrange $args 0 1] == {xview moveto}) ||
                    ([lrange $args 0 1] == {xview scroll}) ||
                    ([lrange $args 0 1] == {yview moveto}) ||
                    ([lrange $args 0 1] == {yview scroll})} {

                    event generate  $widget <<Changed>>
                }

                #return original command
                return $orig_call
            }
            ''')
        self.text.tk.eval('''
            rename {widget} new
            interp alias {{}} ::{widget} {{}} widget_interceptor {widget} new
        '''.format(widget=str(self.text)))
        return


    def binding_keys(self):
        for key in ['<Down>','<Up>',"<<Changed>>","<Configure>"]:
            self.text.bind(key, self.changed)
        self.linenumbers.bind('<Button-1>',self.linenumbers.get_breakpoint_number)
        return

    def changed(self, event):
        self.linenumbers.re_render()
        #print "render"
        return


    def _set_(self):
        self.linenumbers = LineNumberCanvas(self.master, width=30)
        self.linenumbers.connect(self.text)
        self.linenumbers.pack(side="left", fill="y")
        return       


if __name__ == '__main__':
    root = tk.Tk()
    l=tk.Text(root)
    LineMain(l)
    l.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 "LineNumber.py" In Our "MagicStick/magicsticklibs" Project Directory.


Our MagicStick Project Directory Structure.


MagicStick_Editor_Part_3
 |
 |---> About.txt  
 |
 |---> magicsticklibs
 |  |
 |  |---> __init__.py
 |  |     
 |  |---> Main.py
 |  |            
 |  |---> TextPad.py
 |  |
 |  |---> Graphics.py
 |  |
 |               |---> ConfigSettings.py 
 |  |
 |  |---> ScrollBar.py
 |  |
 |  |---> LineNumber.py
 |
 |
 |---> main.py

Step 2.
          Add "LineNumber.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
##
##
## ################################################
## ###### Please Don't Remove Author Name #########
## ############# Thanks ###########################
## ################################################
##
##
__author__='''

######################################################
                By S.S.B Group                          
######################################################

    Suraj Singh
    Admin
    S.S.B Group
    surajsinghbisht054@gmail.com
    http://bitforestinfo.blogspot.com/

    Note: We Feel Proud To Be Indian
######################################################
'''

from LineNumber import LineMain
from ScrollBar import Scrollbar

class Connect:
 def __init__(self, pad):
  self.pad = pad
  self.modules_connections()


 def modules_connections(self):
  LineMain(self.pad)
  Scrollbar(self.pad)
  return

Boom! Our LineNumber.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 "LineNumber.py".


After Running "main.py". my screenshot



This Tutorial Ends Here,

In My Next Tutorial, We will Continue This project.


Written By
        SSB

Share this

Related Posts

Previous
Next Post »