如何从Tkinter中获取Text小部件的数据以便自由使用数据?

huangapple go评论103阅读模式
英文:

How do you grab the Text widget data from Tkinter in order to freely use the data?

问题

我正在尝试利用通过Tkinter的Text小部件输入的文本数据,以与使用Python中的input()方法输入数据的方式相同的方式使用它。

以下是我的示例代码:

root = Tk()
root.geometry("750x250")

def retrieve_input():
    input_value = textBox.get("1.0","end-1c")
    print(input_value)

textBox = Text(root, height=7, width=50)
textBox.pack()
buttonCommit = Button(root, height=1, width=10, text="Commit",
                      command=lambda: retrieve_input())

buttonCommit.pack()

mainloop()

我的问题是,当我输入一些内容并单击按钮时,我可以在终端上看到值被打印出来,但是我无法像循环retrieve_input()函数一样使用它。使用return input_value并不会在使用for循环循环函数时产生任何输出。

简而言之,我想能够像以下示例一样做一些事情:

#.....
def retrieve_input():
    input_value = textBox.get("1.0", "end-1c")
    print(input_value)
    return input_value

for a in retrieve_input():
    print(a)
#.....

这只是一个示例。它可以是任何允许我使用通过Tkinter输入的内容数据的东西。

我在某处读到,你需要在适当的时候运行事件处理程序并捕获数据,但不知道应该如何实现,因为没有提供示例代码。

英文:

I am trying to make use of the input text data that is entered through Tkinter's Text widget, the same way one would be able to use the data, it if the data was entered using the input() method in python.

Here is my sample code:

root=Tk()
root.geometry("750x250")


def retrieve_input():
    input_value = textBox.get("1.0","end-1c")
    print(input_value)


textBox = Text(root, height=7, width=50)
textBox.pack()
buttonCommit = Button(root, height=1, width=10, text="Commit",
                      command=lambda: retrieve_input())

buttonCommit.pack()

mainloop()

My issue is that I can see the values printed on the terminal, when I enter something and click the button, but I am not be able to, for instance, loop over the retrieve_input() function. Doing a return input_value doesn't really result in any output when I loop over the function, using a for loop.

In Short I would like to be able to do something like the following:

    #.....
    def retrieve_input():
        input_value = textBox.get("1.0","end-1c")
        print(input_value)
        return input_value
    
    for a in retrive_input():
       print(a)
   #.....

This is Just an example. It could be anything that allows me to use the content data that I enter through Tkinter.

I read somewhere that you somehow need an event handler to run at the appropriate time and capture the data but no idea how that's supposed to be implemented since no sample code was provided.

答案1

得分: 1

唯一需要做的是a)确保`retrieve_input`返回数据,b)确保在用户有机会输入数据之前不要运行迭代代码。

英文:

The only thing you need to do is a) make sure that retrieve_input returns the data, and b) make sure that the code that iterates over it doesn't run until the user has a chance to enter some data.

from tkinter import *

root=Tk()
root.geometry("750x250")


def retrieve_input():
    input_value = textBox.get("1.0","end-1c")
    return input_value


def process_input():
    for a in retrieve_input():
        print(a)

textBox = Text(root, height=7, width=50)
textBox.pack()
buttonCommit = Button(root, height=1, width=10, text="Commit",
                      command=process_input)
buttonCommit.pack()

mainloop()

huangapple
  • 本文由 发表于 2023年3月7日 01:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定