我正在制作TietaToe游戏,当我们按下一个按钮时,下次就不需要再按下它了。为此,我向所有按钮添加了一个事件,并将按下按钮的状态更改为disabled,其命令更改为None,但仍然有效。当我们按下按钮,它会改变他的尺寸。但是我需要保持以前的尺寸
我的代码:
import tkinter as tk
table = [
[None, None, None],
[None, None, None],
[None, None, None]
]
turn = "red"
root = tk.Tk()
root.title("TieTacToe")
root.geometry("402x450")
def callback(event):
global turn
b = event.widget
if turn == "red":
b["bg"] = "red"
table[int(b['text'].split("x")[0])][int(b['text'].split("x")[1])] = 1
turn = "blue"
b["text"] = "X"
b["font"] = 6
b["state"] = 'disabled'
b["command"] = None
else:
b["bg"] = "blue"
table[int(b['text'].split("x")[0])][int(b['text'].split("x")[1])] = 0
turn = "red"
b["text"] = "O"
b["font"] = 6
b["state"] = 'disabled'
b["command"] = None
print(table)
# table[0][0]
b1 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='0x0')
b1.place(x=85, y=100)
b1.bind("<Button-1>", callback)
# table[0][1]
b2 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='0x1')
b2.place(x=170, y=100)
b2.bind("<Button-1>", callback)
# table[0][2]
b3 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='0x2')
b3.place(x=255, y=100)
b3.bind("<Button-1>", callback)
# table[1][0]
b4 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='1x0')
b4.place(x=85, y=200)
b4.bind("<Button-1>", callback)
# table[1][1]
b5 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='1x1')
b5.place(x=170, y=200)
b5.bind("<Button-1>", callback)
# table[1][2]
b6 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='1x2')
b6.place(x=255, y=200)
b6.bind("<Button-1>", callback)
# table[2][0]
b7 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='2x0')
b7.place(x=85, y=300)
b7.bind("<Button-1>", callback)
# table[2][1]
b8 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='2x1')
b8.place(x=170, y=300)
b8.bind("<Button-1>", callback)
# table[2][2]
b9 = tk.Button(root, padx=20, pady=20, bg='gray', fg='gray', text='2x2')
b9.place(x=255, y=300)
b9.bind("<Button-1>", callback)
root.mainloop()
问题的原因是您对这些按钮使用了
.bind()
。当左键单击这些按钮时,将调用callback
函数,而不是在按下这些按钮时调用过程:
left click => the button is clicked.
您可以使用
.unbind()
。例如:相关问题 更多 >
编程相关推荐