【问题标题】:I change button`s states to "disabled" but it works anyway我将按钮的状态更改为“禁用”,但它仍然有效
【发布时间】:2020-06-21 20:19:48
【问题描述】:

我正在制作 TieTacToe 游戏,当我们按下一个按钮时,我们下次不需要再按下它。为此,我向所有按钮添加了一个事件,并将按下按钮的状态更改为禁用,并将其命令更改为 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()

【问题讨论】:

标签: python user-interface tkinter


【解决方案1】:

问题的原因是你对这些按钮使用了.bind()callback 函数将在左键单击这些按钮时调用,而不是当你按下这些按钮时.

过程: left click =&gt; the button is clicked.

你可以使用.unbind().Like:

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>")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2013-04-09
    • 2023-04-03
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多