Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当某个按钮处于';点击了什么?_Python_Python 3.x_Tkinter_Tic Tac Toe - Fatal编程技术网

Python 当某个按钮处于';点击了什么?

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。您需要将该行分隔为两条语句: 您需

大家好,我正试图用Tkinter创建一个简单的井字游戏,但我面临一个问题,当玩家点击指定按钮时,我无法更新该按钮的图像,我无法使用
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