1

I am trying to bind a button press to a function in Tkinter when I run the following line of code

get = Button(root, lambda: ChangeColour(boxes[1][2], boxes[5][2], 
             main)).pack(fill = BOTH)

I get the following error

SyntaxError: non-keyword arg after keyword arg

Am I missing something, should I be binding the function differently?

EDIT: Stack trace below

File "C:/Users/07725172499/Documents/a.py", line 151
  relief=RIDGE, lambda: ChangeColour(boxes[1][2], boxes[5][2], main)).pack(fill = BOTH)
                                  ^
SyntaxError: non-keyword arg after keyword arg

Process finished with exit code 1

tdelaney
  • 73,364
  • 6
  • 83
  • 116

1 Answers1

1

The error SyntaxError: non-keyword arg after keyword arg occurs when you break the Python requirement that arguments can be given in a mixture of positional order and keyword=value pairs but once a keyword is provided, you cannot subsequently within that function call use positional order. For example, if a function definition begins with

def foo(first, second, third):

then calling the function with

foo(1, 2, 3)

is ok and

foo(1, 2, third=3) 

is ok but

foo(1, second=2, 3)

is not ok.

From your traceback, it looks like you provided a value for the relief parameter by keyword, so you need to provide the function to bind by keyword. Use command=lamda: as the argument. However, I suspect that your call to ChangeColor is returning a value that is not a function and therefore does not match the expected type for the command argument of Button.

To figure out why your code is breaking this syntax requirement, I recommend breaking up the line of code so that you can identify whether it is the call to ChangeColor, pack or Button that is causing the problem. To make your code easier to debug, put each function call on its own line:

bound_function = ChangeColour(boxes[1][2], boxes[5][2], main)
my_button = Button(root, bound_function)
get = my_button.pack(fill=BOTH)
Bennett Brown
  • 5,234
  • 1
  • 27
  • 35