【问题标题】:How to update the image of a specific button when it's clicked on?单击特定按钮时如何更新特定按钮的图像?
【发布时间】:2021-01-27 01:40:38
【问题描述】:
from tkinter import *

# Creating the main window
topper = Tk()
topper.geometry('414x417')
topper.resizable(False, False)
topper.configure(bg='green')

# Importing all images that'll be used
b7_img = PhotoImage(file="C:\\project images\\b7.png")
b8_img = PhotoImage(file="C:\\project images\\b8.png")
b9_img = PhotoImage(file="C:\\project images\\b9.png")
b4_img = PhotoImage(file="C:\\project images\\b4.png")
b5_img = PhotoImage(file="C:\\project images\\b5.png")
b6_img = PhotoImage(file="C:\\project images\\b6.png")
b1_img = PhotoImage(file="C:\\project images\\b1.png")
b2_img = PhotoImage(file="C:\\project images\\b2.png")
b3_img = PhotoImage(file="C:\\project images\\b3.png")
bX_img = PhotoImage(file="C:\\project images\\bX.png")
bO_img = PhotoImage(file="C:\\project images\\bO.png")

# This determines if it's player 1's or player 2's turn to give an input
player_turn = False

# This is the function that's supposed to replace/update the image of the designated button on-click
def btn_click(button):
    global player_turn
    if player_turn:
        button(image=bO_img)
        player_turn = True
    if not player_turn:
        button(image=bX_img)
        player_turn = False


b7 = Button(topper, height='114', width='115', image=b7_img, command=lambda: btn_click(b7)).place(x=0, y=0)
b8 = Button(topper, height='114', width='115', image=b8_img, command=lambda: btn_click(b8)).place(x=150, y=0)
b9 = Button(topper, height='114', width='115', image=b9_img, command=lambda: btn_click(b9)).place(x=300, y=0)
b4 = Button(topper, height='114', width='115', image=b4_img, command=lambda: btn_click(b4)).place(x=0, y=150)
b5 = Button(topper, height='114', width='115', image=b5_img, command=lambda: btn_click(b5)).place(x=150, y=150)
b6 = Button(topper, height='114', width='115', image=b6_img, command=lambda: btn_click(b6)).place(x=300, y=150)
b1 = Button(topper, height='114', width='115', image=b1_img, command=lambda: btn_click(b1)).place(x=0, y=300)
b2 = Button(topper, height='114', width='115', image=b2_img, command=lambda: btn_click(b2)).place(x=150, y=300)
b3 = Button(topper, height='114', width='115', image=b3_img, command=lambda: btn_click(b3)).place(x=300, y=300)


topper.mainloop()

大家好,我正在尝试使用 Tkinter 创建一个简单的井字游戏,但我遇到了一个问题,当玩家点击它时,我无法更新指定按钮的图像,我不能使用btn_click 函数更改指定按钮的image 关键字参数,无论何时我尝试它都会给TypeError: 'NoneType' object is not callable

有什么方法可以解决这个问题,而不必为每个按钮创建一个专门的函数?谢谢。

【问题讨论】:

    标签: python python-3.x tkinter tic-tac-toe


    【解决方案1】:

    您的代码中存在三个问题:

    1. place()(始终为None)的结果分配给bX,如b7 = Button(...).place(...)。您需要将该行分成 2 个语句:
    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)
    
    1. 您需要使用button.config(image=...) 来更改image 选项,而不是btn_click() 函数中的button(image=...)

    2. 您为player_turn 分配了错误的值(当它已经是True 时分配True,当它已经是False 时分配False)。

    更新btn_click()函数:

    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
    

    我认为按钮在被点击后应该被禁用,这样就不能再次被点击了。

    【讨论】:

    • 感谢您的回答,但有没有办法可以禁用按钮而不将其颜色更改为灰色?
    • 您无法更改禁用的背景,但您可以将command 选项重置为"",而不是禁用按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多