1

I am new to python, and self taught. I am trying to make a simplifed minesweeper game called "Mines". This game has a 5 by 5 grid of buttons, with a random number of mines. Everything is working fine, except for one issue. After you click a safe space, the button should be disabled so you cannot gain any more points. With my code, I have no idea how to do this.

I did already try to call a method to disable the button, as well as using the DISABLED command. Neither worked. I want the "white" buttons to be disabled after click, as those are the safe spaces. In a perfect world, I will call the disable() method when the button is clicked and use that method to disable that button.

##Create the canvas of the board.
window = Tk ()
window.title("Mines Game")
window.geometry('819x655')
##Scoring function. The first box clicked awards 1 point, and after that each box is worth double your total points.
def score():
    global scores
    if (scores == 0):
        scores = scores + 1
        print ("Safe space hit! You have "   + str(scores) + " point.")
    else:
        scores = scores * 2 
        print ("Safe space hit! You have "   + str(scores) + " points.")
def squares():
    ##Main method of creating a 5x5 square of buttons.
    global scores
    scores = 0
    less_bombs = True
    r = -1
    c = 0
    x = 0
    if less_bombs:
        for i in range(5):
            r = r + 1
            for l in range(5):
                y = randint(0,25)
                if x == 5:
                    y = 10
                if y < 6:
                    btn = Button(window, bg = "red", command=end)
                    btn.grid(column = c,row = r)
                    #btn.grid(sticky="nesw")
                    btn.config(height = 8, width = 22)
                    x = x + 1
                else:
                    btn = Button(window, bg = "white", command=lambda:[score(),DISABLED])
                    btn.grid(column = c,row = r)
                    #btn.grid(sticky="nesw")
                    btn.config(height = 8, width = 22)
                c = c + 1
            c = 0
def end():
    ##This method creates the popup after you click a mine.
    end = Tk ()
    end.title ('Game Over!')
    end.geometry ('300x161')
    btn = Button(end, text ="Close game", command=close)
    btn.grid(column = 0,row = 0)
    #btn.grid(sticky="nesw")
    btn.config(height = 10, width = 20)
    btn = Button(end, text ="Reset game", command=lambda:[reset(),end.destroy()])
    btn.grid(column = 1,row = 0)
    #btn.grid(sticky="nesw"
    btn.config(height = 10, width = 20)
    if (scores == 1):
        print ("Game over! You hit a mine :(")
        print ("Your score for that game was "  + str(scores) + " point.")
    if (scores != 1):
        print ("Game over! You hit a mine :(")
        print ("Your score for that game was "  + str(scores) + " points.")
def disable():
    pass
def close():
    sys.exit()
def reset():
    squares()
squares()
window.mainloop()

I want the button to be disabled after being clicked, however currently the player can click the button as many times as he or she wants.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • I think your question was already asked and answered here https://stackoverflow.com/questions/16046743/how-to-change-tkinter-button-state-from-disabled-to-normal – Abhishta Gatya Mar 24 '19 at 17:56
  • Possible duplicate of [How to change Tkinter Button state from disabled to normal?](https://stackoverflow.com/questions/16046743/how-to-change-tkinter-button-state-from-disabled-to-normal) – Abhishta Gatya Mar 24 '19 at 17:57
  • Unless I'm implementing it wrong, this isn't my solution. Not only am I trying to do the opposite, I'm trying to disable one button out of my 25. – Christian Edwards Mar 24 '19 at 18:30

1 Answers1

0

You can fix it by changing creation of white Buttonss as indicated below in ALL CAPS. This gives the lambda function a default value for btn which changes each iteration of loop (otherwise the function would always refer to the last one created in the for loop).

def squares():
    """ Main method of creating a 5x5 square of buttons. """
    global scores
    scores = 0
    less_bombs = True
    r = -1
    c = 0
    x = 0
    if less_bombs:
        for i in range(5):
            r = r + 1
            for l in range(5):
                y = randint(0,25)
                if x == 5:
                    y = 10
                if y < 6:
                    btn = Button(window, bg="red", command=end)
                    btn.grid(column=c, row=r)
                    #btn.grid(sticky="nesw")
                    btn.config(height=8, width=22)
                    x = x + 1
                else:
                    btn = Button(window, bg="white")  ## REMOVE command= OPTION. ##
                    btn.grid(column=c, row=r)
                    #btn.grid(sticky="nesw")
                    btn.config(height=8, width=22, # USE command SHOWN BELOW. ##
                        command=lambda btn=btn: [score(), btn.config(state=DISABLED)])
                c = c + 1
            c = 0
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Christian: Glad to hear. Suggest you read (and start following) the [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/). – martineau Mar 24 '19 at 19:11