2017-01-18 58 views
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()  

回答

0

onclick不知道哪个按钮是你的意思,你没有通过它指向一个特定的按钮。所以它只能使用最近分配给btn的东西,在右下角的按钮中。