我正在 Tkinter 中开发一个简单的文本编辑器。我想在用户输入文本时验证文本小部件,以便可以应用样式。我无法绑定,<KeyPress>因为它在按下按键后更新,并且我无法绑定<KeyRelease>,因为当用户按住按键时它不会触发事件,而这是它需要的。还有其他办法解决这个问题吗?
这是最小的代码:
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>",lambda event: self.keyPress())
self.text.focus()
def keyPress(self):
#This is not called when a key is held down
print(self.text.get("end-2c","end-1c"))
root = tk.Tk()
root.title("Text editor test")
t = textEditor(root)
t.pack()
root.mainloop()
慕慕森
相关分类