问题描述
如何从 Text
小部件获取 Tkinter 输入?
How to get Tkinter input from the Text
widget?
编辑
我问这个问题是为了帮助遇到同样问题的其他人 - 这就是没有示例代码的原因.这个问题困扰了我好几个小时,我用这个问题来教别人.请不要把它当作一个真正的问题来评价——答案才是最重要的.
I asked this question to help others with the same problem - that is the reason why there is no example code. This issue had been troubling me for hours and I used this question to teach others. Please do not rate it as if it was a real question - the answer is the thing that matters.
推荐答案
要从文本框中获取 Tkinter 输入,您必须向普通的 .get()
函数添加更多属性.如果我们有一个文本框 myText_Box
,那么这就是检索其输入的方法.
To get Tkinter input from the text box, you must add a few more attributes to the normal .get()
function. If we have a text box myText_Box
, then this is the method for retrieving its input.
def retrieve_input():
input = self.myText_Box.get("1.0",END)
第一部分,"1.0"
意味着输入应该从第一行,字符零(即:第一个字符)读取.END
是一个导入的常量,它被设置为字符串 "end"
.END
部分表示一直读到文本框的末尾.唯一的问题是它实际上为我们的输入添加了一个换行符.因此,为了修复它,我们应该将 END
更改为 end-1c
(感谢 Bryan Oakley) -1c
删除 1 个字符,而 -2c
表示删除两个字符,依此类推.
The first part, "1.0"
means that the input should be read from line one, character zero (ie: the very first character). END
is an imported constant which is set to the string "end"
. The END
part means to read until the end of the text box is reached. The only issue with this is that it actually adds a newline to our input. So, in order to fix it we should change END
to end-1c
(Thanks Bryan Oakley) The -1c
deletes 1 character, while -2c
would mean delete two characters, and so on.
def retrieve_input():
input = self.myText_Box.get("1.0",'end-1c')
这篇关于如何从 Tkinter Text Widget 获取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!