1 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
我可以通過(guò)簡(jiǎn)單地將<KeyRelease>和綁定<KeyPress>到tk.Text小部件來(lái)實(shí)現(xiàn)它。(雖然我按照bindings指定的相同順序進(jìn)行了操作,但我認(rèn)為這不會(huì)產(chǎn)生影響)。
現(xiàn)在我不能說(shuō)這是一個(gè)好的解決方案還是一個(gè)好的實(shí)踐,但它確實(shí)有效。
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.bind("<KeyPress>",lambda event: self.keyPress())
self.text.focus()
def keyPress(self):
print(self.text.get("end-2c","end-1c"))
root = tk.Tk()
root.title("Text editor test")
t = textEditor(root)
t.pack()
root.mainloop()
添加回答
舉報(bào)