0

I am trying to create a minesweeper-esque game in tkinter. The goal is to click on a button, randomly pick a number, and, if that number is 1, give the player a point. The problem is that I want the button you click on to be disabled and it's colour changed depending on whether the 'cat' was found or not. The only button that does this is the bottom right one that alternates between colours and becomes disabled, even when that isn't the one you clicked on. I'm not sure where the problem is, so I'd appreciate the help.

from tkinter import *
from random import *
turns=10
points=0
def onclick():
    global turns,points
    iscat=randint(1,11)
    btn.configure(state="disabled")
    if iscat==1:
        btn.configure(background="blue")
        statlabel.configure(text="You found a cat!")
        points=points+1
    else:
        btn.configure(bg="red")
        statlabel.configure(text="It's empty! Hurry, or all the cats will die!")
    turns=turns-1
root=Tk()
root.title("Catsweeper")
root.configure(background="black")
frame=Frame(root)
frame.configure(bg="black")
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
frame.grid(row=0, column=0)
grid=Frame(frame)
grid.grid(column=0, row=7, columnspan=2)
Grid.rowconfigure(frame, 7, weight=1)
Grid.columnconfigure(frame, 0, weight=1)
chosenx=int(input("How many rows? "))
choseny=int(input("How many columns? "))
for x in range(1,chosenx+1):
    for y in range(1, choseny+1):
        btn=Button(frame, command=onclick, state = "normal")
        btn.grid(column=x, row=y)
statlabel=Label(frame, text="", background="red", fg="white")
statlabel.grid(column=choseny+1)
if turns==0:
    statlabel.configure(text="GAME OVER")
    btn.configure(state="disabled")
root.mainloop()    
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

The onclick has no idea which button you mean, you're not passing it a reference to a specific button. So it's only able to use the most recent thing assigned to btn, which in your case in the bottom right button.

cssko
  • 3,027
  • 1
  • 18
  • 21