1

I want to make a button that displays a label when clicked. Here was my thinking:

from Tkinter import *

def write_hello():
    w = Label(root, text = "Hello. This is a label.")
    w.pack()

root = Tk()

button = Button(root, text = "Hello!", command = write_hello() )
button.pack()

root.mainloop()

Can anyone be nice enough to walk me through this? I'm very new.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Brandon
  • 321
  • 1
  • 4
  • 10

1 Answers1

2

You're calling the write_hello function before you even create the button, so what you're probably seeing is the label showing up before the button on the UI (and without clicking it). What you want to do is pass the function to the Button constructor instead of the function's return value:

button = Button(root, text = "Hello!", command = write_hello)
mgilson
  • 300,191
  • 65
  • 633
  • 696