Код конечно же сырой, сильно не пинать.
import tkinter as tk root = tk.Tk() root.title("Текстовый редактор") scrollbar = tk.Scrollbar() scrollbar.pack(side='right', fill='y') text_widget = tk.Text(root) text_widget.pack(side="right", expand=True, fill="both") text_widget.focus_set() line_numbers = tk.Text(root, bg="#f0f0f0", fg='black', width=4, bd=0, highlightthickness=0) line_numbers.pack(side="left", fill="y") def on_update(event): line_numbers.delete(1.0, "end") lines = text_widget.get(1.0, "end").split("\n") for i, lines in enumerate(lines, 1): line_numbers.insert("end", str(i)+'\n') # line_feed() #text_widget.insert(tk.END, 2*"\n") #print("lines= ", lines) def on_delete(event): ndex=text_widget.index('insert') dex=ndex.split('.')[-1] if int(dex) == 0 and float(ndex) != 1.0: end_str=line_numbers.get("end-1l", "end") if end_str == '\n': line_numbers.delete("end-1c", "end") line_numbers.delete("end-1l", "end") else: line_numbers.delete("end-1l", "end") else: return def on_return(event): # ln_txt=line_numbers.index("end-1c") # print(ln_txt) # line_numbers.insert("end", ln_txt.split('.')[0]+'\n') end_line=line_numbers.get("end-1l", "end") if end_line == '\n': line=line_numbers.get(1.0, "end").split('\n') print(line) count=enumerate(line, 1) num_str=int(line[-3])+1 print(num_str) line_numbers.insert("end", str(num_str)+'\n') else: line_numbers.insert("end", '\n') line=line_numbers.get(1.0, "end").split('\n') count=enumerate(line, 1) num_str=int(line[-3])+1 line_numbers.insert("end", str(num_str)+'\n') # def line_feed(): # line_numbers.insert("end", "5\n") # strok=['end-1l', 'end-2l', 'end-3l', 'end-4l', 'end-5l', 'end-6l', 'end-7l', 'end-8l', 'end-9l'] # #i=0 # #n=strok[i] # for i in range(0, 8): # n=strok[i] # ln=line_numbers.get(str(n), "end") # if ln== '\n': # print("n= ", n) # else: return # i=i+1 # print("i= ", i) def on_scrollbar(*args): '''прокручивает оба текста при скроллинге''' line_numbers.yview(*args) text_widget.yview(*args) def on_textscroll(*args): '''передача аргумента скроллинга''' scrollbar.set(*args) on_scrollbar('moveto', args[0]) text_widget.bind("<<Modified>>", on_update) text_widget.bind("<Return>", on_return) text_widget.bind("<BackSpace>", on_delete) # text_widget.bind("<Button-4>", on_scrollbar) # text_widget.bind("<Button-5>", on_scrollbar) scrollbar['command'] = on_scrollbar line_numbers['yscrollcommand'] = on_textscroll text_widget['yscrollcommand'] = on_textscroll root.mainloop()
Есть идеи?
А, да – написано всё под Линём.