-1

This is my code, I am trying to make it so that when I press a button, a specific text shows up

from tkinter import *

def greet():
    label.config(text = "Hi there!")

root = Tk()
root.title("Placing a button")

button = Button(root, text = "Click here", command = greet).pack()
label = Label(root, text = "   n ").pack()





root.mainloop()

However, when I run it and click the button, I get this error:

label.config(text = "Hi there!")

AttributeError: 'NoneType' object has no attribute 'config'

I would really appreciate help.

1 Answers1

0

Using .pack() on your label will return None. This means

label = Label(root, text = "   n ").pack()

is now label = None. In order to do what you want to do you need to edit your code to this

from tkinter import *

def greet():
    label.config(text = "Hi there!")

root = Tk()
root.title("Placing a button")

button = Button(root, text = "Click here", command = greet).pack()
label = Label(root, text = "   n ")
label.pack()





root.mainloop()

This way label is no longer None.

kusz
  • 367
  • 1
  • 9