Tkinter,Entry 小部件,是否可以检测输入文本?

编程入门 行业动态 更新时间:2024-10-27 03:26:24
本文介绍了Tkinter,Entry 小部件,是否可以检测输入文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一个简单的计算器上有一个 Entry 小部件.用户可以选择通过键盘输入方程式.我想知道是否有一种方法可以检测到输入到 Entry 小部件中的字符(在我的情况下来自键盘).所以,焦点在小部件上,用户按下4",它出现在小部件上......我可以检测到这种行为,以记录输入的基本目的吗?

I have an Entry widget on a simple calculator. The user can choose to enter an equation via the keypad. I was wondering if there was a way to detect a character(from the keypad in my case) being typed into the Entry widget. So, focus is on the widget, user presses '4', it comes up on the widget... can I detect this act, for basic purposes of logging the input?

推荐答案

每次在 Tkinter 窗口内按下一个键,都会创建一个 Tkinter.Event 实例.您需要做的就是访问该实例.这是一个简单的脚本,演示了如何:

Every time you press a key inside a Tkinter window, a Tkinter.Event instance is created. All you need to do is access that instance. Here is a simple script that demonstrates just how:

from Tkinter import Tk, Entry root = Tk() def click(key): # print the key that was pressed print key.char entry = Entry() entry.grid() # Bind entry to any keypress entry.bind("<Key>", click) root.mainloop()

key(作为一个 Tkinter.Event 实例)包含许多不同的属性,可用于在按下的键上获取几乎任何类型的数据.我选择在这里使用 .char 属性,这将使脚本打印每个按键是什么.

key (being a Tkinter.Event instance) contains many different attributes that can be used to get almost any type of data you want on the key that was pressed. I chose to use the .char attribute here, which will have the script print what each keypress is.

更多推荐

Tkinter,Entry 小部件,是否可以检测输入文本?

本文发布于:2023-07-20 17:55:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   文本   Tkinter   Entry

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!