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

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!

Here is the code below

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

Any help would be appreciated!


Answer: What you have to do is to bind a Button event to the copytextButton object
like so:

    
    
    copytextButton.bind('<Button-1>', copytext)
    

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

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

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

Edit: This line is not needed:

    
    
    copytextButton.configure(command=copytext())
    

