Here is the code for a simple text editor, complete with a numberline on the side. The numbers themselves are a bit buggy. However, the problem is that when i make alot of lines, the numberline and the actuall text widget gets skewed. (see picture below)
from tkinter import*
import random
class Main(Tk):
def __init__(self):
super().__init__()
self.scroll = Scrollbar(self)
self.scroll.pack(fill=Y, expand=True, side=RIGHT)
self.numberLine = Text(self, width=5)
self.numberLine.pack(side=LEFT)
self.txt = Text(self)
self.txt.pack(side=LEFT)
self.scroll["command"] = self.onScrollbar
self.txt["yscrollcommand"] = self.ontextScroll
self.numberLine["yscrollcommand"] = self.ontextScroll
self.txt.bind('<Key>', self.insert)
def onScrollbar(self, *args):
self.txt.yview(*args)
self.numberLine.yview(*args)
def ontextScroll(self, *args):
self.scroll.set(*args)
self.onScrollbar("moveto", args[0])
def insert(self, b):
self.txt.configure(state="disabled")
self.numberLine.delete("1.0", "end")
for i in range(int(self.txt.index('end').split(".")[0])):
self.numberLine.insert(INSERT, f'{i}\n')
self.txt.configure(state="normal")
app = Main()
app.mainloop()