tkinter Entry() 不返回字符串

发布于 2025-01-11 19:55:43 字数 544 浏览 0 评论 0原文

我有几个用 tk 制作的输入框: Entry()

我需要将用户输入的内容放入变量中,我这样做(正如我在网上找到的):

window = Tk()

#make entry and turn it into stringvar
entry1string = tk.StringVar
entry_1 = Entry(window,textvariable=entry1string)

#retrieve it into a variable
retrieved = entry1string.get()

这给出了以下错误:

AttributeError: 'str' object has no attribute 'get'

如何将用户输入到输入框中的字符串/值获取到变量中?该代码似乎就是我找到的每个示例的样子,我不明白为什么它会给我这个错误。

I have several entry boxes made with tk: Entry()

I need to put what the user enters into a variable, which I do as such (as I have found online):

window = Tk()

#make entry and turn it into stringvar
entry1string = tk.StringVar
entry_1 = Entry(window,textvariable=entry1string)

#retrieve it into a variable
retrieved = entry1string.get()

This gives the following error:

AttributeError: 'str' object has no attribute 'get'

How do I get the string/value entered into the entry box by the user into a variable? The code seems to be just how every example I've found is, I don't see why it's giving me that error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2025-01-18 19:55:43

请参阅此处了解Tkinter 中的条目小部件

您可以做的是创建一个按钮,单击该按钮后,将检索在“条目”框中输入的数据。

...
entry1string = tk.StringVar()
entry_1 = Entry(window,textvariable=entry1string).pack()

def retrieveData():
    #retrieve it into a variable
    retrieved = entry1string.get()
    #print the data
    print(retrieved)
    #Or output the data on the window in a Label :
    Label(window, text=retrieved).pack()

button1 = Button(window, text="PRINT", command=retrieveData).pack()

单击按钮时需要调用一个函数。您可以在命令行中打印数据,甚至可以在 GUI 窗口上输出,这是您的选择。

阅读文档了解更多信息。

Refer here to know about Entry widgets in Tkinter.

What you can do is create a button, on which upon clicking, the data entered in the Entry box will be retrieved.

...
entry1string = tk.StringVar()
entry_1 = Entry(window,textvariable=entry1string).pack()

def retrieveData():
    #retrieve it into a variable
    retrieved = entry1string.get()
    #print the data
    print(retrieved)
    #Or output the data on the window in a Label :
    Label(window, text=retrieved).pack()

button1 = Button(window, text="PRINT", command=retrieveData).pack()

There needs to be a function to call when the button is clicked. you can print the data in the command line or even output on the GUI window, that's your choice.

Read the documentation to know more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文