Python/Tkinter: How to set text widget contents to the value of a variable?

时光毁灭记忆、已成空白 提交于 2019-12-12 14:20:02

问题


I am writing a program to assist with a trivial part of my job that can be automated. My purpose here is to:

  1. Copy and paste a chunk of plain text into a Tkinter text widget

  2. 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')

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.


回答1:


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 only, 3 requires that "END" be END from the Tk namespace.




回答2:


def  set_value():
    text.insert("end-1c",value)


来源:https://stackoverflow.com/questions/30957085/python-tkinter-how-to-set-text-widget-contents-to-the-value-of-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!