как делать отступы после переноса строки в Text tkinter
Можно ли в tkinter реализовать возможность автоматически добавлять отступы после переноса строки? Если да, то как?
Например мы ввели
текст
И нужно чтобы после того как мы нажали ENTER, добавилось такое же количество пробелов, что и в предыдущей строке ...
Ответы (2 шт):
Автор решения: Victor VosMottor
→ Ссылка
Вообщем-то как-то так. но надо сильно допилить. Если что — спрашивайте в комментах.
import tkinter as tk
import re
root = tk.Tk()
text = tk.Text(root)
text.pack(fill="both", expand=True)
def autoindent(event):
# the text widget that received the event
widget = event.widget
# get current line
line = widget.get("insert linestart", "insert lineend")
# compute the indentation of the current line
match = re.match(r'^(\s+)', line)
current_indent = len(match.group(0)) if match else 0
# compute the new indentation
new_indent = current_indent + 4
# insert the character that triggered the event,
# a newline, and then new indentation
widget.insert("insert", event.char + "\n" + " "*new_indent)
# return 'break' to prevent the default behavior
return "break"
text.bind(":", autoindent)
root.mainloop()
Автор решения: Вадим Платонов
→ Ссылка
Я составил небольшой код
from tkinter import *
c=Tk()
text1=''
tabs=0
def tab(event):
global tabs,text1
tabs=0
back_line=(text1.get('insert linestart','insert lineend'))
for i in back_line:
if i== ' ':
tabs+=1
else:
break
cut=' '*(tabs)
text1.insert('insert','\n')
text1.insert('insert',(cut))
return 'break'
text1=Text(c,wrap='none')
text1.pack(fill=BOTH)
text1.bind('<Return>',tab)
c.mainloop()
(Для тех кто в такой-же ситуации)