0

I am very new to Python. As a way of learning Tkinter to apply to other projects, I wanted to create something that would update a label to what was in a entry after a button press.

from tkinter import *

top = Tk()
response = StringVar()
response.set("text")

var = StringVar()
var.set("Input")

def updateLabel():
    response.set(ent.get())

ent = Entry(top, textvariable = var)
lab = Label(top, textvariable = response)
but = Button(top, text = "Enter", command = updateLabel())

lab.pack(side = LEFT)
ent.pack(side = LEFT)
but.pack(side = RIGHT)
top.mainloop()

It seems like the function is run without me pressing the button because right when the run it, the label already says "Input" like the entry.

Any help would be appreciated. I am sure it is a stupid mistake.

Thanks!

1 Answers1

2

It's because you're calling updateLabel() by including the parentheses in the command= option, try this instead:

but = Button(top, text="Enter", command=updateLabel)

the command= option expects a callable (aka pre-parentheses)

Tresdon
  • 1,211
  • 8
  • 18
  • Sure thing, if this solved your problem please mark the answer as accepted so others can find the solution more quickly :) – Tresdon Jul 01 '20 at 19:58