字体用四个空格代替制表符



我想创建一个文本小部件,该选项卡替换为四个空格。虽然@martineau的答案有类似的东西,但它真正做的只是让标签看起来更小。我想要实际的空间。我该怎么做呢?我不能使用链接问题中的第一个函数因为它会干扰自动补全

代码:

from tkinter import *
from tkinter.ttk import *
keywords_list = ["from", "tkinter", "import", "Label", "Button", "Entry"]
class specialText(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.text = Text(self, width=50, height=10)
self.text.pack(fill=BOTH, expand=True)
self.text.insert(END, "# This is a test. Autocomplete works")
self.text.bind("<Any-KeyRelease>", self._autocomplete)
self.text.bind("<Tab>", self._handle_tab, add=True)
def callback(self, word):
#Returns possible matches
#words is a list of almost every keyword and builtin function
words = keywords_list
matches = [x for x in words if x.startswith(word)]
return matches
def _handle_tab(self, event):
tag_ranges= self.text.tag_ranges("autocomplete")
if tag_ranges:
self.text.mark_set("insert", tag_ranges[1])
self.text.tag_remove("sel", "1.0", "end")
self.text.tag_remove("autocomplete", "1.0", "end")
return "break"
def _autocomplete(self, event):
if event.char and self.callback and event.keysym != "BackSpace":
word = self.text.get("insert-1c wordstart", "insert-1c wordend")
matches = self.callback(word)
if matches:
remainder = matches[0][len(word):]
insert = self.text.index("insert")
self.text.insert(insert, remainder, ("sel", "autocomplete"))
self.text.mark_set("insert", insert)
if __name__ == "__main__":
window = Tk()
window.title("Autocomplete")
window.geometry("500x500")
text = specialText(window)
text.pack(fill=BOTH, expand=True)
window.mainloop()

我明白了

代码:

from tkinter.ttk import *
keywords_list = ["from", "tkinter", "import", "Label", "Button", "Entry"]
class specialText(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.text = Text(self, width=50, height=10)
self.text.pack(fill=BOTH, expand=True)
self.text.insert(END, "# This is a test. Autocomplete works")
self.text.bind("<Any-KeyRelease>", self._autocomplete)
self.text.bind("<Tab>", self._handle_tab, add=True)
def callback(self, word):
#Returns possible matches
#words is a list of almost every keyword and builtin function
words = keywords_list
matches = [x for x in words if x.startswith(word)]
return matches
def _handle_tab(self, event):
tag_ranges= self.text.tag_ranges("autocomplete")
if tag_ranges:
self.text.mark_set("insert", tag_ranges[1])
self.text.tag_remove("sel", "1.0", "end")
self.text.tag_remove("autocomplete", "1.0", "end")
return "break"
# Here's the difference:
else:
self.text.insert("    ")
return "break"
def _autocomplete(self, event):
if event.char and self.callback and event.keysym != "BackSpace":
word = self.text.get("insert-1c wordstart", "insert-1c wordend")
matches = self.callback(word)
if matches:
remainder = matches[0][len(word):]
insert = self.text.index("insert")
self.text.insert(insert, remainder, ("sel", "autocomplete"))
self.text.mark_set("insert", insert)
if __name__ == "__main__":
window = Tk()
window.title("Autocomplete")
window.geometry("500x500")
text = specialText(window)
text.pack(fill=BOTH, expand=True)
window.mainloop()

最新更新