I'd like to pass 2 variable arguments in a function to be assigned to a Button
command
. These variables change within a for
loop that also creates the buttons.
Mostly inspired by some of the top answers here and here, here are my fail attempts at trying to solve this problem based on what I read:
I tried using partial
:
self.dct[(i, j)] = Button(command=partial(self.on_click, i, j))
Another try:
self.dct[(i, j)] = Button(command=partial(partial(self.on_click, i), j))
And another:
self.dct[(i, j)] = Button(command=partial(self.on_click, [i, j]))
.. Guess what?
tup = [i, j]
self.dct[(i, j)] = Button(command=partial(self.on_click, tup))
And then, lambda
:
self.dct[(i, j)] = Button(command=lambda i=i, j=j: self.on_click(i, j))
Here's my code:
import tkinter as tk
from functools import partial
class Board(tk.Frame):
board = None
images = None
tile = None
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.tile = {}
for i in range(10):
for j in range(10):
self.tile[(i, j)]['btn_obj'] = tk.Button(self.board, command=partial(partial(self.on_click, i), j))
def on_click(self, i, j):
print("X: {}, Y:{}".format(j, i))
partial
always causes an error like this:
TypeError: on_click() takes 2 positional arguments but 3 were given
It's always mismatched number of arguments.
Meanwhile, lambda
gets the wrong value of the variables, causing something of an error in tkinter
's part.