Update a label in tkinter from a button press
<p>My question is regarding GUI programming in python by using tkinter. I believe this is Python 3x. </p>

<p>My question: While we're executing a program to run the GUI, can a button update a label? More specifically, is there a way to change the labels displayed text after pushing the button? I have consulted stack overflow on this before and adopted the StringVar() method, but it doesn't seem to fix my problem, in fact it omits the text from the GUI completely!</p>

<p>Here is the code below</p>

<pre><code>from tkinter import *

root = Tk()
root.title('Copy Text GUI Program')

copiedtext = StringVar()
copiedtext.set("Text is displayed here")


def copytext():
    copiedtext.set(textentered.get())

# Write 'Enter Text Here'
entertextLabel = Label(root, text="Enter Text Here")
entertextLabel.grid(row=0, column=0)

# For the user to write text into the gui
textentered = Entry(root)
textentered.grid(row=0, column=1)

# The Copy Text Button
copytextButton = Button(root, text="Copy Text")
copytextButton.grid(row=1, columnspan=2)

# Display the copied text
displaytextLabel = Label(root, textvariable=copiedtext)
displaytextLabel.grid(row=2,columnspan=2)


copytextButton.configure(command=copytext())

root.mainloop()
</code></pre>

<p>Any help would be appreciated!</p>

<p>What you have to do is to bind a Button event to the copytextButton object like so:</p>

<pre><code>copytextButton.bind('&lt;Button-1&gt;', copytext)
</code></pre>

<p>This means that a callback function - copytext() will be called when you left-click the button.</p>

<p>A slight modification is needed in the function itself, since the callback sends an event argument:</p>

<pre><code>def copytext(event):
    copiedtext.set(textentered.get())
</code></pre>

<p>Edit:
This line is not needed:</p>

<pre><code>copytextButton.configure(command=copytext())
</code></pre>

python tkinter