python tkinter中点击回车清空Text,同时光标显示在0.0(转载自 https://blog.csdn.net/dcyywin8/article/details/83306011)

所遇问题:

当想要使用Text中的绑定事件回车清空Text中的内容时,总是先执行清空操作,再执行回车操作,这样每次Text其它内容都清空了,但还是会留下一个回车。

思路:

使用bind方法获取键盘的事件,当键盘事件(event)的keycode等于13时,触发事件。
新建一个线程作为被触发事件。
在线程中,使用time模块的sleep函数进行等待(等待Text中的回车事件结束),之后使用delete方法删除Text中所有文本。
 

示例:

def func_thrd_ExecuteCommand():
time.sleep(0.01)
self.txt_.delete(0.0, END)
def handle_Input(event):
if event.keycode==13:
thrd_once=threading.Thread(target=func_thrd_ExecuteCommand)
thrd_once.start()
txt_ = Text(root)
txt_.bind('<Key>',func=handle_Input)
txt_.pack(side=BOTTOM, padx=0, fill='both', expand=NO)
 
---------------------
作者:井盖上的青蛙
来源:CSDN
原文:https://blog.csdn.net/dcyywin8/article/details/83306011
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/shiyongge/p/10226672.html