Python 当某个按钮处于';点击了什么?
大家好,我正试图用Tkinter创建一个简单的井字游戏,但我面临一个问题,当玩家点击指定按钮时,我无法更新该按钮的图像,我无法使用Python 当某个按钮处于';点击了什么?,python,python-3.x,tkinter,tic-tac-toe,Python,Python 3.x,Tkinter,Tic Tac Toe,大家好,我正试图用Tkinter创建一个简单的井字游戏,但我面临一个问题,当玩家点击指定按钮时,我无法更新该按钮的图像,我无法使用btn\u click功能更改指定按钮的image关键字参数,每当我尝试调用时,它都会给出TypeError:“NoneType”对象不可调用 有没有什么方法可以解决这个问题,而不必为每个按钮创建专门的功能?谢谢。您的代码中有三个问题: 将place()(始终为None)的结果以类似b7=按钮(…).place(…)的方式分配给bX。您需要将该行分隔为两条语句: 您需
btn\u click
功能更改指定按钮的image
关键字参数,每当我尝试调用时,它都会给出TypeError:“NoneType”对象不可调用
有没有什么方法可以解决这个问题,而不必为每个按钮创建专门的功能?谢谢。您的代码中有三个问题:
place()
(始终为None
)的结果以类似b7=按钮(…).place(…)
的方式分配给bX
。您需要将该行分隔为两条语句:button.config(image=…)
来更改image
选项,而不是按钮(image=…)
内部的btn\u click()
函数
player\u turn
(当值已经True
时分配True
,当值已经False
时分配False
)
btn\u click()
功能:
b7 = Button(topper, height='114', width='115', image=b7_img, command=lambda: btn_click(b7))
b7.place(x=0, y=0)
b8 = Button(topper, height='114', width='115', image=b8_img, command=lambda: btn_click(b8))
b8.place(x=150, y=0)
b9 = Button(topper, height='114', width='115', image=b9_img, command=lambda: btn_click(b9))
b9.place(x=300, y=0)
b4 = Button(topper, height='114', width='115', image=b4_img, command=lambda: btn_click(b4))
b4.place(x=0, y=150)
b5 = Button(topper, height='114', width='115', image=b5_img, command=lambda: btn_click(b5))
b5.place(x=150, y=150)
b6 = Button(topper, height='114', width='115', image=b6_img, command=lambda: btn_click(b6))
b6.place(x=300, y=150)
b1 = Button(topper, height='114', width='115', image=b1_img, command=lambda: btn_click(b1))
b1.place(x=0, y=300)
b2 = Button(topper, height='114', width='115', image=b2_img, command=lambda: btn_click(b2))
b2.place(x=150, y=300)
b3 = Button(topper, height='114', width='115', image=b3_img, command=lambda: btn_click(b3))
b3.place(x=300, y=300)
我认为应该在单击按钮后将其禁用,以便不能再次单击它。您的代码中有三个问题:
place()
(始终为None
)的结果以类似b7=按钮(…).place(…)
的方式分配给bX
。您需要将该行分隔为两条语句:button.config(image=…)
来更改image
选项,而不是按钮(image=…)
内部的btn\u click()
函数
player\u turn
(当值已经True
时分配True
,当值已经False
时分配False
)
btn\u click()
功能:
b7 = Button(topper, height='114', width='115', image=b7_img, command=lambda: btn_click(b7))
b7.place(x=0, y=0)
b8 = Button(topper, height='114', width='115', image=b8_img, command=lambda: btn_click(b8))
b8.place(x=150, y=0)
b9 = Button(topper, height='114', width='115', image=b9_img, command=lambda: btn_click(b9))
b9.place(x=300, y=0)
b4 = Button(topper, height='114', width='115', image=b4_img, command=lambda: btn_click(b4))
b4.place(x=0, y=150)
b5 = Button(topper, height='114', width='115', image=b5_img, command=lambda: btn_click(b5))
b5.place(x=150, y=150)
b6 = Button(topper, height='114', width='115', image=b6_img, command=lambda: btn_click(b6))
b6.place(x=300, y=150)
b1 = Button(topper, height='114', width='115', image=b1_img, command=lambda: btn_click(b1))
b1.place(x=0, y=300)
b2 = Button(topper, height='114', width='115', image=b2_img, command=lambda: btn_click(b2))
b2.place(x=150, y=300)
b3 = Button(topper, height='114', width='115', image=b3_img, command=lambda: btn_click(b3))
b3.place(x=300, y=300)
我认为应该在单击按钮后将其禁用,以便不能再次单击。谢谢您的回答,但是有没有办法在不将按钮颜色更改为灰色的情况下禁用按钮?您不能更改禁用的背景,但可以将
命令
选项重置为“”
而不是禁用按钮。谢谢您的回答,但是有没有办法在不将按钮颜色更改为灰色的情况下禁用按钮?您不能更改禁用的背景,但可以将命令
选项重置为”
而不是禁用按钮。
def btn_click(button):
global player_turn
if player_turn:
button.config(image=bO_img)
else:
button.config(image=bX_img)
player_turn = not player_turn