0
我正在嘗試在tkinter中創建掃雷類遊戲。目標是點擊一個按鈕,隨機挑選一個數字,如果這個數字是1,則給玩家一分。問題是我想讓你點擊的按鈕被禁用,並根據是否找到'貓'來改變顏色。唯一可以做到這一點的按鈕是右下角的按鈕,它在顏色之間交替變成禁用狀態,即使那不是您點擊的按鈕。我不確定問題出在哪裏,所以我會很感激幫助。爲什麼我的按鈕不能正常工作?
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()