I'm pretty new to python, but I'm trying to create a simplistic version of Mineweeper. I have developed functions that randomly place mines in a 9x9 matrix (nested lists) and then I have functions that assign values to every cell that doesn't contain a mine corresponding to the number of mines a given cell touches. This should make sense to anyone familiar with the game.
Anyway, I've started creating the GUI using Tkinter, which I am completely new to, and have run into a problem. As you can see, I have two nested for loops that create 81 buttons in Tkinter, these make up the grid that the user will interact with to "mine sweep". I've assigned them all to a list and, on mouse click, I have it call a function called "swept" which is where I will place all of the logic for determining if the user has clicked on a mine, etc. The problem is that I don't know how to figure out just which button has been clicked from inside the "swept" function. Any thoughts?
I'm sure there's a much more elegant way to create this game, but I'd like to keep going with the way I have come up with so far. Below I've added the code that I have written for the GUI.
from Tkinter import *
class App:
def __init__(self,master):
global rows
global grid
frame = Frame(master)
frame.grid()
r1 = []; r2 = []; r3 = []; r4 = []; r5 = []; r6 = []; r7 = []; r8 = []; r9 = [];
grid = [r1,r2,r3,r4,r5,r6,r7,r8,r9]
for x in range(0,9):
for y in range(0,9):
grid[x].append(Button(frame, text=" ", command = self.swept, height = 1,width = 2))
grid[x][y].grid(row=x, column=y)
## for x in range(0,9):
## print rows[x]
def swept(self):
## Don't know how to determine which button has been pressed from inside this function.
print self
root = Tk()
app = App(root)
root.mainloop()