我想制作一个简单的文本编辑器,在我输入时显示唯一单词的数量加上总字数。我尝试过使用t.get("1.0","end-1c")
,但我不知道如何进行交互(实时)。来自相关答案的玩具代码,其中“按键”功能尝试将文本小部件的内容打印到终端,是:
import tkinter as tk
class textEditor(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.textFrm = tk.Frame(self)
self.textFrm.pack(fill = "x")
self.text = tk.Text(self.textFrm, relief = "flat", font = ("Arial","11"))
self.text.pack(fill = "both", expand = True)
self.text.bind('<KeyRelease>', self.keyPress())
self.text.focus()
def keyPress(self):
words = self.text.get("1.0","end-1c")
print(words)
root = tk.Tk()
root.title("Text editor test")
t = textEditor(root)
t.pack()
root.mainloop()