如何在tkinter的标签上显示用户输入?

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

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()

如何在tkinter的标签上显示用户输入?

huangapple
  • 本文由 发表于 2023年5月21日 00:24:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296213.html
匿名

发表评论

匿名网友

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

确定