Advertisement
here2share

# line_number_to_text_widget.py

May 27th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. # line_number_to_text_widget.py
  2.  
  3. import Tkinter as tk
  4.  
  5. class CustomText(tk.Text):
  6.     def __init__(self, *args, **kwargs):
  7.         tk.Text.__init__(self, *args, **kwargs)
  8.  
  9.         # create a proxy for the underlying widget
  10.         self._orig = self._w + "_orig"
  11.         self.tk.call("rename", self._w, self._orig)
  12.         self.tk.createcommand(self._w, self._proxy)
  13.  
  14.     def _proxy(self, *args):
  15.         # let the actual widget perform the requested action
  16.         cmd = (self._orig,) + args
  17.         result = self.tk.call(cmd)
  18.  
  19.         # generate an event if something was added or deleted,
  20.         # or the cursor position changed
  21.         if (args[0] in ("insert", "replace", "delete") or
  22.             args[0:3] == ("mark", "set", "insert") or
  23.             args[0:2] == ("xview", "moveto") or
  24.             args[0:2] == ("xview", "scroll") or
  25.             args[0:2] == ("yview", "moveto") or
  26.             args[0:2] == ("yview", "scroll")
  27.         ):
  28.             self.event_generate("<<Change>>", when="tail")
  29.  
  30.         # return what the actual widget returned
  31.         return result        
  32.  
  33. class TextLineNumbers(tk.Canvas):
  34.     def __init__(self, *args, **kwargs):
  35.         tk.Canvas.__init__(self, *args, **kwargs)
  36.         self.textwidget = None
  37.  
  38.     def attach(self, text_widget):
  39.         self.textwidget = text_widget
  40.  
  41.     def redraw(self, *args):
  42.         '''redraw line numbers'''
  43.         self.delete("all")
  44.  
  45.         i = self.textwidget.index("@0,0")
  46.         while True :
  47.             dline= self.textwidget.dlineinfo(i)
  48.             if dline is None: break
  49.             y = dline[1]
  50.             linenum = str(i).split(".")[0]
  51.             self.create_text(2,y,anchor="nw", text=linenum)
  52.             i = self.textwidget.index("%s+1line" % i)
  53.  
  54. class Example(tk.Frame):
  55.     def __init__(self, *args, **kwargs):
  56.         tk.Frame.__init__(self, *args, **kwargs)
  57.         self.text = CustomText(self)
  58.         self.vsb = tk.Scrollbar(orient="vertical", command=self.text.yview)
  59.         self.text.configure(yscrollcommand=self.vsb.set)
  60.         self.text.tag_configure("bigfont", font=("Helvetica", "24", "bold"))
  61.         self.linenumbers = TextLineNumbers(self, width=30)
  62.         self.linenumbers.attach(self.text)
  63.  
  64.         self.vsb.pack(side="right", fill="y")
  65.         self.linenumbers.pack(side="left", fill="y")
  66.         self.text.pack(side="right", fill="both", expand=True)
  67.  
  68.         self.text.bind("<<Change>>", self._on_change)
  69.         self.text.bind("<Configure>", self._on_change)
  70.  
  71.         self.text.insert("end", "one\ntwo\nthree\n")
  72.         self.text.insert("end", "four\n",("bigfont",))
  73.         self.text.insert("end", "five\n")
  74.  
  75.     def _on_change(self, event):
  76.         self.linenumbers.redraw()
  77.  
  78. if __name__ == "__main__":
  79.     root = tk.Tk()
  80.     Example(root).pack(side="top", fill="both", expand=True)
  81.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement