Home > other >  Why isn't a label with entry widget value being displayed properly?
Why isn't a label with entry widget value being displayed properly?

Time:06-06

I tried a lot to solve this, but the message "<bound method StringVar.get of <tkinter.StringVar object at 0x00000000024FB8E0>>" keeps being displayed, the .get() method wasn't working either for some reason, here is the code:

root.title("Testing this thing :D")
v = tk.StringVar()
label1 = tk.Label(root,text="Write your name").pack()
entrybox = tk.Entry(root,font= ("Arial 12"),textvariable=v).pack(expand=True)
def showmsg():
    label = tk.Label(root, text = f"Hello {v.get}",font=("Arial 12")).pack()
button = tk.Button(root,font=("Arial 12"), command= showmsg(),text="Done").pack(fill= X)
root.mainloop()

CodePudding user response:

There are two problems in your code.

  1. In Button, You give command=showmsg() Here you call this function This should be command=showmsg

  2. In function showmsg you wrote var.get This should be var.get().


import tkinter as tk

root = tk.Tk()

root.title("Testing this thing :D")
v = tk.StringVar()
label1 = tk.Label(root,text="Write your name").pack()
entrybox = tk.Entry(root,font= ("Arial 12"),textvariable=v).pack(expand=True)
def showmsg():
    label = tk.Label(root, text = f"Hello {v.get()}",font=("Arial 12")).pack()
button = tk.Button(root,font=("Arial 12"), command= showmsg,text="Done").pack(fill= tk.X)
root.mainloop()
  • Related