1

I am trying to make a program that when I'll write something in the entry and click a button it'll pin up ("hello" + what I've entered in the entry ).It worked when I didn't make it in a variable and also when I made it in a variable and the variable is inside a function, but it didn't work when I made it in a variable but I didn't make it in any function.

from tkinter import *

root = Tk()
root.title("Problems")
root.geometry("1080x720")
root.config(bg="blue")

entry = Entry(root,bg="purple",fg="black")
entry.pack()

Hello = "Hello " + entry.get() + "!"

def click():
   myLabel = Label(root, text = Hello)
   myLabel.pack()
Button = Button(root,text="enter your name",command=click,bg="Black",fg="white")
Button.pack()

root.mainloop()

1 Answer 1

2

You are defining your Hello variable before the user could type in the entry. You have to do it like this:

from tkinter import *

root = Tk()
root.title("Problems")
root.geometry("1080x720")
root.config(bg="blue")

myentry = Entry(root,bg="purple",fg="black")
myentry.pack()

def click():
    Hello = 'Hello ' + entry.get() + '!'
    myLabel = Label(root, text = Hello)
    myLabel.pack()

mybutton = Button(root, text="enter your name", command=click, bg="Black", fg="white")
mybutton.pack()

root.mainloop()

Edit: Avoid overnaming variables

Like you have written: Button = Button(...) Now you will get a error if you try to make a new button

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.