Я новичок в Python и самоучка. Я пытаюсь сделать упрощенную игру тральщика под названием «Шахты». Эта игра имеет сетку кнопок 5 на 5 со случайным количеством мин. Все работает нормально, кроме одного вопроса. После того, как вы щелкнете по безопасному месту, кнопка должна быть отключена, чтобы вы больше не могли набирать очки. С моим кодом я понятия не имею, как это сделать.
Я уже пытался вызвать метод для отключения кнопки, а также с помощью команды DISABLED. Ни один не работал. Я хочу, чтобы "белые" кнопки были отключены после щелчка, так как это безопасные места. В идеальном мире я вызываю метод disable () при нажатии кнопки и использую этот метод для отключения этой кнопки.
##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()
Я хочу, чтобы кнопка была отключена после нажатия, однако в настоящее время игрок может нажимать кнопку столько раз, сколько он или она хочет.