0
from tkinter import *

root = Tk()
root.title('printer')
label = Label(root, text = "WELCOME", fg = "grey")

label.pack()
textbox = Entry(root)
textbox.pack()


def whenclicked():
    global hello
    hello = "textbox.get()"
    label1 = Label(root, text = hello)
    label1.pack()



button = Button(root, text = "print it", command = whenclicked())
button.pack()
root.mainloop()

when I run this code , before I click the button the output is already present . what is wrong in this?

peter
  • 104
  • 8

2 Answers2

1

All arguments of a function are evaluated before the function call, so when you call:

Button(root, text = "print it", command=whenclicked())

it first calls whenclicked() and then passes its result to the Button constructor. The command argument should be a function, so pass it just like that:

Button(root, text = "print it", command=whenclicked)

(Unrelated, but quotes in hello = "textbox.get()" are redundant, if you want to get the textbox content.)

bereal
  • 32,519
  • 6
  • 58
  • 104
-2

it looks like label might be part of mainloop which could cause it to function apart from the rest of your code without interaction take it out of mainloop.

  • still it is not working . can you give a little more clarification – peter Jun 04 '20 at 06:37
  • It looks like your trying to run it before its declared in the code if its in mainloop its always going to be running like a light switch without an on off! – ScubaToaster Jun 04 '20 at 06:42