Python 3 Tkinter Adding Line Numbers to Text Widget in GUI Desktop App

Python 3 Tkinter Adding Line Numbers to Text Widget in GUI Desktop App

Python 3 Tkinter Adding Line Numbers to Text Widget in GUI Desktop App

import tkinter as tk


class LineNumbers(tk.Text):
    def __init__(self, master, text_widget, **kwargs):
        super().__init__(master, **kwargs)

        self.text_widget = text_widget
        self.text_widget.bind('<KeyPress>', self.on_key_press)

        self.insert(1.0, '1')
        self.configure(state='disabled')

    def on_key_press(self, event=None):
        final_index = str(self.text_widget.index(tk.END))
        num_of_lines = final_index.split('.')[0]
        line_numbers_string = "n".join(str(no + 1) for no in range(int(num_of_lines)))
        width = len(str(num_of_lines))

        self.configure(state='normal', width=width)
        self.delete(1.0, tk.END)
        self.insert(1.0, line_numbers_string)
        self.configure(state='disabled')


if __name__ == '__main__':
    w = tk.Tk()
    t = tk.Text(w)
    l = LineNumbers(w, t, width=1)
    l.pack(side=tk.LEFT)
    t.pack(side=tk.LEFT, expand=1)
    w.mainloop()
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment