-3

I'm working through a youtube tutorial on Tkinter. Everything has been going fine but now I've hit a roadblock. The guy is showing how to use the .get() method to create a label with the text the user enters into an entry when the user hits a button. Save from the names of the label/entry/button, all my code is identical to his in function. It works for him, but I get a nonetype error. Here is a link to the video with a timestamp. Can anyone help me please?

URL: https://www.youtube.com/watch?v=YXPyB4XeYLA Time: 33:30

from tkinter import *
import random

root = Tk()
root.title("Simple Routines")   

Entry1 = Entry(root, width=20, bg='grey').grid(row=2, column=1)

def onClick():
    Name = Entry1.get()
    Label2 = Label(root, text=Name)
    Label2.grid(row=4, column=1)

Label1 = Label(root, text="What Is Your Name?").grid(row=1, column=1)
Button1 = Button(root, text="Enter Your Name", bg='grey', command=onClick).grid(row=3, column=1)

root.mainloop()

1 Answers1

1

This is because you have used .grid() on your textbox in the same line. In Tkinter if you place your widgets in the same line in which you create them it makes the widgets inaccessible later. I have attached the code for your reference.

from tkinter import *
import random

root = Tk()
root.title("Simple Routines")   

Entry1 = Entry(root, width=20, bg='grey')
Entry1.grid(row=2, column=1) # The change

def onClick():
    Name = Entry1.get()
    Label2 = Label(root, text=Name)
    Label2.grid(row=4, column=1)

Label1 = Label(root, text="What Is Your Name?").grid(row=1, column=1)
Button1 = Button(root, text="Enter Your Name", bg='grey', command=onClick).grid(row=3, column=1)

root.mainloop()