Reputation: 375
Below is the code that I am using to make a chessboard out of buttons.
from Tkinter import *
for x in xrange(8):
for y in xrange(8:
if((x+y)%2 == 0):
Button(root, bg="white", width=11, height=5).grid(row=y, column=x)
else:
Button(root, bg="black", width=11, height=5).grid(row=y, column=x)
I know how to make a callback function for an individual button, but I am not sure how I would go about implementing a callback function for each of these 64 buttons so that, when pressed, they will return their position in the grid.
Upvotes: 1
Views: 3574
Reputation: 18858
Try binding the x and y values for each button to a lambda that can call a handler function whenever a button is pressed. Now you have the x and y position of each button press.
def handlebuttonpress(x,y):
print 'Button x-{0} y-{1} pressed'.format(x,y)
width, height = 8, 8
for x in xrange(width):
for y in xrange(height):
if((x+y)%2 == 0):
Button(root, command=lambda x=x, y=y: handlebuttonpress(x,y), bg="white", width=11, height=5).grid(row=y, column=x)
else:
Button(root, command=lambda x=x, y=y: handlebuttonpress(x,y), bg="black", width=11, height=5).grid(row=y, column=x)
Upvotes: 1
Reputation: 880797
Edit: I think I like @DonQuestion's event-based approach better because it does not require a different function for each button.
Below I've adapted my original code to use
master.bind("<Button-1>", self.onclick)
to react to mouse clicks (instead of using tk.Button(command = ...)
import Tkinter as tk
class ButtonEventBlock(object):
# http://stackoverflow.com/a/6102759/190597
def __init__(self, master, names, cols):
self.names = names
self.cols = cols
self.button = []
for i, name in enumerate(names):
self.button.append(tk.Button(master, text = name))
row, col = divmod(i, self.cols)
self.button[i].grid(sticky = tk.W+tk.E+tk.N+tk.S,
row = row, column = col, padx = 1, pady = 1)
master.bind("<Button-1>", self.onclick)
def onclick(self, event):
info = event.widget.grid_info()
# print(info)
# {'rowspan': '1', 'column': '3', 'sticky': 'nesw', 'ipady': '0', 'ipadx':
# '0', 'columnspan': '1', 'in': <Tkinter.Tk instance at 0xab3d7ec>,
# 'pady': '1', 'padx': '1', 'row': '0'}
row, col = [int(info[key]) for key in ['row', 'column']]
i = self.cols*row + col
print(row, col, self.names[i])
names = ('One', 'Two', 'Three', 'Four', 'Five',
'Six', 'Seven', 'Eight', 'Nine', 'Ten')
root = tk.Tk()
ButtonEventBlock(root, names, cols = 5)
root.mainloop()
Upvotes: 0
Reputation: 11624
def callback(event):
x,y = event.widget.grid_location()
This example should point you into the right direction.
update: to clarify the usage of grid_location i did a quick-google and found ... a SO-post ;-) which put me to shame by providing exactly your needed solution in a more direct way:
grid_info = event.widget.grid_info()
print "row:", grid_info["row"], "column:", grid_info["column"]
so the credit should go to Bryan Oakley ;-) and this question may be called a duplicate ...
Upvotes: 3