Tkinter:设置StringVar之后事件,包括按下的键

Tkinter:设置StringVar之后事件,包括按下的键,第1张

Tkinter:设置StringVar之后 事件,包括按下的键

一个简单的解决方案是在类绑定之后添加新的bindtag。这样,类绑定将在绑定之前触发。见这个答案的问题如何Tkinter的文本部件绑定自事件发生后,将通过文本控件绑定?举个例子
该答案使用输入小部件而不是文本小部件,但是绑定标记的概念在这两个小部件之间是相同的。只要确保使用

Text
而不是
Entry
适当的地方即可。

另一个解决方案是在KeyRelease上进行绑定,因为默认绑定发生在KeyPress上。

这是显示如何使用绑定标签的示例:

import Tkinter as tkclass Example(tk.frame):    def __init__(self, master):        tk.frame.__init__(self, master)        self.post_tweet = tk.Text(self)        bindtags = list(self.post_tweet.bindtags())        bindtags.insert(2, "custom") # index 1 is where most default bindings live        self.post_tweet.bindtags(tuple(bindtags))        self.post_tweet.bind_class("custom", "<Key>", self.count)        self.post_tweet.grid()        self.char_count = tk.Label(self)        self.char_count.grid()    def count(self, event):        current = len(self.post_tweet.get("1.0", "end-1c"))        remaining = 140-current        self.char_count.configure(text="%s characters remaining" % remaining)if __name__ == "__main__":    root = tk.Tk()    Example(root).pack(side="top", fill="both", expand=True)    root.mainloop()


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5644635.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存