I am having problems with tkinter in python

Viewed 45

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 Answers

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

Related