Python Forum

Full Version: pressing the button
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
It looks like the click event does not get called and so textbox2 does not get populated with the value of textbox1.
Do you see why?
Thank you

from tkinter import *

def click():
    name = textbox1.get()
    message = str("Hello " + name)
    textbox2["text"] = message

window = Tk()
window.title("Names")
window.geometry("450x350")
window.configure(background="black")

label1 = Label(text="Enter your name:")
label1.place(x=30, y=200)
label1["bg"] = "black"
label1["fg"] = "white"

textbox1 = Entry(text = "")
textbox1.place(x=150, y=200, width=200, height=25)
textbox1["justify"] = "center"
textbox1.focus()

button1 = Button(text="Press me", command=click)
button1.place(x=30, y=250, width=120, height=25)
button1["bg"] = "yellow"

textbox2 = Entry(text="")
textbox2.place(x=150, y=250, width=200, height=25)
textbox2["bg"] = "white"
textbox2["fg"] = "black"

window.mainloop()
If you add a print statement to the click event handler you will see that it is actually getting called.
The problem is that the entry contents are not being set correctly.
If you see this link https://tkdocs.com/tutorial/widgets.html#entry you will find that delete and insert methods are used to change the contents of an entry, this can also be done by using a textvariable.

On a side note, you should learn to use classes as it makes structuring GUI code easier.
Also see Namespace flooding with * imports
(Nov-13-2021, 11:44 AM)Yoriz Wrote: [ -> ]If you add a print statement to the click event handler you will see that it is actually getting called.
The problem is that the entry contents are not being set correctly.
If you see this link https://tkdocs.com/tutorial/widgets.html#entry you will find that delete and insert methods are used to change the contents of an entry, this can also be done by using a textvariable.

On a side note, you should learn to use classes as it makes structuring GUI code easier.
Also see Namespace flooding with * imports
Thank you