Python 我将按钮的状态更改为;残疾人士;但不管怎样,它还是有效的
我正在制作TietaToe游戏,当我们按下一个按钮时,下次就不需要再按下它了。为此,我向所有按钮添加了一个事件,并将按下按钮的状态更改为disabled,其命令更改为None,但仍然有效。当我们按下按钮,它会改变他的尺寸。但是我需要保持以前的尺寸 我的代码:Python 我将按钮的状态更改为;残疾人士;但不管怎样,它还是有效的,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在制作TietaToe游戏,当我们按下一个按钮时,下次就不需要再按下它了。为此,我向所有按钮添加了一个事件,并将按下按钮的状态更改为disabled,其命令更改为None,但仍然有效。当我们按下按钮,它会改变他的尺寸。但是我需要保持以前的尺寸 我的代码: import tkinter as tk table = [ [None, None, None], [None, None, None], [None, None, None] ] turn = "r
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()
将tkinter作为tk导入
表=[
[无,无,无],
[无,无,无],
[无,无,无]
]
转向=“红色”
root=tk.tk()
根标题(“TieTacToe”)
根几何(“402x450”)
def回调(事件):
全球转向
b=event.widget
如果转弯==“红色”:
b[“背景”]=“红色”
表[int(b['text'].split(“x”)[0])][int(b['text'].split(“x”)[1])]=1
turn=“蓝色”
b[“文本”]=“X”
b[“字体”]=6
b[“状态”]=“已禁用”
b[“命令”]=无
其他:
b[“背景”]=“蓝色”
表[int(b['text'].split(“x”)[0])][int(b['text'].split(“x”)[1])]=0
转向=“红色”
b[“文本”]=“O”
b[“字体”]=6
b[“状态”]=“已禁用”
b[“命令”]=无
打印(表格)
#表[0][0]
b1=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='0x0')
b1.地点(x=85,y=100)
b1.绑定(“,回调)
#表[0][1]
b2=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='0x1')
b2.地点(x=170,y=100)
b2.bind(“,回调)
#表[0][2]
b3=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='0x2')
b3.位置(x=255,y=100)
b3.bind(“,回调)
#表[1][0]
b4=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='1x0')
b4.地点(x=85,y=200)
b4.bind(“,回调)
#表[1][1]
b5=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='1x1')
b5.地点(x=170,y=200)
b5.bind(“,回调)
#表[1][2]
b6=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='1x2')
b6.位置(x=255,y=200)
b6.bind(“,回调)
#表[2][0]
b7=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='2x0')
b7.位置(x=85,y=300)
b7.bind(“,回调)
#表[2][1]
b8=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='2x1')
b8.位置(x=170,y=300)
b8.bind(“,回调)
#表[2][2]
b9=tk.按钮(根,padx=20,pady=20,bg='gray',fg='gray',text='2x2')
b9.位置(x=255,y=300)
b9.bind(“,回调)
root.mainloop()
问题的原因是您对这些按钮使用了.bind()
。callback
函数将在左键单击这些按钮时调用,而不是在按下这些按钮时调用
过程:
左键单击=>按钮被单击。
您可以使用.unbind()
。例如:
def callback(event):
global turn
b = event.widget
if turn == "red":
table[int(b['text'].split("x")[0])][int(b['text'].split("x")[1])] = 1
b["bg"] = "blue"
turn = "blue"
b["text"] = "X"
b["font"] = 6
b["state"] = 'disabled' # Actually, if you want to make you button clickable, you could just remove the line.
b.unbind("<Button-1>") # just do unbind
else:
table[int(b['text'].split("x")[0])][int(b['text'].split("x")[1])] = 0
b["bg"] = "red"
turn = "red"
b["text"] = "O"
b["font"] = 6
b["state"] = 'disabled'
b.unbind("<Button-1>")
def回调(事件):
全球转向
b=event.widget
如果转弯==“红色”:
表[int(b['text'].split(“x”)[0])][int(b['text'].split(“x”)[1])]=1
b[“背景”]=“蓝色”
turn=“蓝色”
b[“文本”]=“X”
b[“字体”]=6
b[“state”]=“disabled”#实际上,如果你想让你的按钮可点击,你可以删除该行。
b、 解绑(“”)#只要解绑就行了
其他:
表[int(b['text'].split(“x”)[0])][int(b['text'].split(“x”)[1])]=0
b[“背景”]=“红色”
转向=“红色”
b[“文本”]=“O”
b[“字体”]=6
b[“状态”]=“已禁用”
b、 解除绑定(“”)
这是否回答了您的问题?