Python Forum
[Tkinter] pressing the button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] pressing the button
#1
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()
Yoriz write Nov-13-2021, 11:24 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
arkiboys likes this post
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] Pressing button by code after gui is loaded KaiBehncke 1 1,412 Nov-18-2019, 10:04 PM
Last Post: Denni
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,029 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020