英文:
How do I display the user entry in a label on tkinter?
问题
playernamelabel = Label(win, text=nameentry.get())
英文:
I stored the entry in a entry widget called nameeentry but how do I display the user's entry in a label? I tried to use the normal label syntax and add command = nameentry but it keeps saying nameentry is not defined in the error
nameentry = ttk.Entry(win,width= 40)
nameentry.pack()
#send text button
ttk.Button(win, text= "Send Text",width= 20).pack(pady=20)
#how to display user entry in a label after this??
nameentry.get()
#here I want to add some code that stores this nameentry in a label but its not working...
playernamelabel=Label(win,text=nameentry)
win.mainloop() #end of window main loop
答案1
得分: 2
If you want a label to contain exactly the same thing that is in an Entry
widget, the simplest solution is to tie them together with the same textvariable
. If they share the same textvariable
, the two will always display the same data.
英文:
If you want a label to contain exactly the same thing that is in an Entry
widget, the simplest solution is to tie them together with the same textvariable
. If they share the same textvariable
, the two will always display the same data.
import tkinter as tk
root = tk.Tk()
var = tk.StringVar(root)
entry = tk.Entry(root, textvariable=var)
label = tk.Label(root, textvariable=var)
entry.pack(side="top", fill="x", padx=8, pady=8)
label.pack(side="top", fill="x", padx=8, pady=8)
entry.insert(0, "Hello, world")
tk.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论