-1
    refreshButton = Button(frameList, text ="Refresh",command = print("pressed"))
    refreshButton.place(x=50, y=50)

Why does this code not print out "pressed" everytime i press the button but only once when the button is created?

There are no error messages. the problem is that print("pressed") does not execute.

Peanutpower
  • 1
  • 1
  • 3

2 Answers2

1

use lambda:

refreshButton = Button(frameList,
    text ="Refresh",
    command = lambda: print("pressed")
  )
lycuid
  • 2,555
  • 1
  • 18
  • 28
0

A button command can't give any parameters unless you use lambda or some other function. Instead you need to make your own function to call when the button is pressed.

def Refresh(*args):
    print("pressed")
    # do stuff

refreshButton = Button(frameList, text ="Refresh",command = Refresh)
refreshButton.place(x=50, y=50)

If all you want to do is print("pressed") then this is the alternative solution. lambda will catch any parameters given by the button and let you call a function with your own parameters :)

refreshButton = Button(frameList, text ="Refresh",command= lambda *args: print("pressed"))
Tom Fuller
  • 5,291
  • 7
  • 33
  • 42