Python/Tkinter:如何将文本小部件内容设置为变量的值? [英] Python/Tkinter: How to set text widget contents to the value of a variable?
问题描述
我正在编写一个程序来帮助我完成工作中可以自动化的一个微不足道的部分.我的目的是:
I am writing a program to assist with a trivial part of my job that can be automated. My purpose here is to:
复制一段纯文本并将其粘贴到 Tkinter 文本小部件中
Copy and paste a chunk of plain text into a Tkinter text widget
使用粘贴的文本块作为变量的值,以便变量可以将某些字符拉出并返回到行中.
Use that pasted chunk of text as the value of a variable so that the variable can have certain characters pulled and returned down the line.
我有一些可以运行的代码.例如,这是我的文本小部件和我用来获取和打印其内容的代码行:
I have a functioning little bit of code. For example, here is my text widget and the lines of code I use to get and print its contents:
textBox = Text(root)
textBox.focus_set()
def get_input():
print textBox.get(1.0, 'end-1c')
然后我使用一个使用命令 get_input
的按钮.它适用于打印小部件的内容.
Then I use a button that uses the command get_input
. It works when it comes to printing the contents of the widget.
现在我知道如何正确调用内容并获取"它们,我想了解如何将这些内容(字符串)分配给变量的值.
Now that I know how to properly call on the contents and 'get' them, I would like to learn how I can assign those contents (a string) to the value of a variable.
推荐答案
我认为你想要的是这个,它会删除所有文本,然后插入一个变量.
I think what you want is this, it will delete all the text, then insert a variable.
def set_input(value):
text.delete(1.0, "END")
text.insert("END", value)
仅适用于 Python 2.x,3 要求 Tk 命名空间中的END"为 END.
Python 2.x only, 3 requires that "END" be END from the Tk namespace.
这篇关于Python/Tkinter:如何将文本小部件内容设置为变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!