因此,当人们单击我创建的按钮时,我尝试显示他们在输入字段上编写的文本,但由于某种原因,当我单击它显示的按钮时:
.!entry
我不知道我做错了什么,因为我是 python 新手,所以我想知道如何解决这个问题,这是我的代码,感谢您的帮助!
from tkinter import *
screen = Tk()
def print_input():
text2 = Label(screen, text=input_field)
text2.grid(row=1, columnspan=3)
text = Label(screen, text="Write to print:")
text.grid(row=0, column=0)
input_field = Entry(screen)
input_field.grid(row=0, column=1)
submit_button = Button(screen, text="Print!", fg="yellow", bg="purple",
command=print_input)
submit_button.grid(row=0, column=2)
screen.mainloop()
最佳答案
更改:
def print_input():
text2 = Label(screen, text=input_field)
至:
def print_input():
text2 = Label(screen, text=input_field.get())
# ^^^^^^
您告诉我们将标签的文本设置为 Entry 小部件,而不是 Entry 小部件的内容。要获取 Entry 小部件的内容,请使用 .get()
方法。
您在标签中看到的时髦字符串是 Entry 小部件的 tkinter 名称。
关于python - Python 中的 Tkinter 输入/条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47820406/