Python Can';不要通过点击按钮来改变按钮的颜色,每个按钮都是由一个函数创建的

Python Can';不要通过点击按钮来改变按钮的颜色,每个按钮都是由一个函数创建的,python,function,tkinter,Python,Function,Tkinter,我们希望在单击按钮时更改按钮的颜色,但问题是它只更改最后一个按钮的颜色,因为我们使用一个函数创建了所有按钮。 问题是是否有任何解决此问题的选项 from tkinter import * main = Tk() main.title("HANGMAN") main.configure(width="800", height="400") main.geometry("800x800") def check(Event): lbutton.config(state="disabled"

我们希望在单击按钮时更改按钮的颜色,但问题是它只更改最后一个按钮的颜色,因为我们使用一个函数创建了所有按钮。 问题是是否有任何解决此问题的选项

from tkinter import *

main = Tk()
main.title("HANGMAN")
main.configure(width="800", height="400")
main.geometry("800x800")

def check(Event):
    lbutton.config(state="disabled", background="red")

def letter(x, gc, gr):
    global lbutton
    c = "lightblue"
    lbutton = Button(frame2, text = x, width="3", height="2", bg=c, activebackground="lightblue")
    lbutton.grid(row=gr,column=gc)
    return lbutton

main.resizable(FALSE,FALSE)
frame1 = Frame(main, width="600",height="600")
frame1.grid(row=0, sticky=E)
frame2 = Frame(main,bg="grey",width="200")
frame2.grid(row=1, sticky=E)

letter("A", 1, 0).bind("<ButtonRelease-1>", check)
letter("B", 2, 0).bind("<ButtonRelease-1>", check)
letter("C", 3, 0).bind("<ButtonRelease-1>", check)
letter("D", 4, 0).bind("<ButtonRelease-1>", check)
letter("E", 5, 0).bind("<ButtonRelease-1>", check)
letter("F", 6, 0).bind("<ButtonRelease-1>", check)
letter("G", 7, 0).bind("<ButtonRelease-1>", check)
letter("H", 8, 0).bind("<ButtonRelease-1>", check)
letter("I", 9, 0).bind("<ButtonRelease-1>", check)
letter("J", 10, 0).bind("<ButtonRelease-1>", check)
letter("K", 11, 0).bind("<ButtonRelease-1>", check)
letter("L", 12, 0).bind("<ButtonRelease-1>", check)
letter("M", 13, 0).bind("<ButtonRelease-1>", check)
letter("N", 1, 1).bind("<ButtonRelease-1>", check)
letter("O", 2, 1).bind("<ButtonRelease-1>", check)
letter("P", 3, 1).bind("<ButtonRelease-1>", check)
letter("Q", 4, 1).bind("<ButtonRelease-1>", check)
letter("R", 5, 1).bind("<ButtonRelease-1>", check)
letter("S", 6, 1).bind("<ButtonRelease-1>", check)
letter("T", 7, 1).bind("<ButtonRelease-1>", check)
letter("U", 8, 1).bind("<ButtonRelease-1>", check)
letter("V", 9, 1).bind("<ButtonRelease-1>", check)
letter("W", 10, 1).bind("<ButtonRelease-1>", check)
letter("X", 11, 1).bind("<ButtonRelease-1>", check)
letter("Y", 12, 1).bind("<ButtonRelease-1>", check)
letter("Z", 13, 1).bind("<ButtonRelease-1>", check)

main.mainloop()
从tkinter导入*
main=Tk()
主要标题(“刽子手”)
main.configure(width=“800”,height=“400”)
主要几何图形(“800x800”)
def检查(事件):
lbutton.config(state=“disabled”,background=“red”)
def字母(x、gc、gr):
全局按钮
c=“浅蓝色”
lbutton=按钮(frame2,text=x,width=“3”,height=“2”,bg=c,activebackground=“lightblue”)
lbutton.grid(行=gr,列=gc)
返回按钮
main.可调整大小(FALSE,FALSE)
框架1=框架(主框架,宽度=“600”,高度=“600”)
frame1.grid(行=0,粘滞=E)
frame2=帧(主,bg=“灰色”,width=“200”)
frame2.grid(行=1,粘滞=E)
字母(“A”,1,0).bind(“,check)
字母(“B”,2,0)。绑定(“,检查)
字母(“C”,3,0)。绑定(“,检查)
字母(“D”,4,0)。绑定(“,检查)
字母(“E”,5,0)。装订(“,支票)
字母(“F”,6,0)。绑定(“,检查)
字母(“G”,7,0)。装订(“,支票)
字母(“H”,8,0)。绑定(“,检查)
字母(“I”,9,0)。装订(“,支票)
字母(“J”,10,0)。装订(“,支票)
字母(“K”,11,0)。绑定(“,检查)
字母(“L”,12,0)。装订(“,支票)
字母(“M”,13,0)。装订(“,支票)
字母(“N”,1,1)。装订(“,支票)
字母(“O”,2,1)。装订(“,支票)
字母(“P”,3,1)。装订(“,支票)
字母(“Q”,4,1)。装订(“,支票)
字母(“R”,5,1)。装订(“,支票)
字母(“S”,6,1)。装订(“,支票)
字母(“T”,7,1)。装订(“,支票)
字母(“U”,8,1)。装订(“,支票)
字母(“V”,9,1)。装订(“,支票)
字母(“W”,10,1)。装订(“,支票)
字母(“X”,11,1)。装订(“,支票)
字母(“Y”,12,1)。装订(“,支票)
字母(“Z”,13,1)。装订(“,支票)
main.mainloop()
我们无法解决此问题,因为web上没有此类信息。
如果您能帮助我们,我们将不胜感激。

最简单的修复方法是在您的函数
中使用
事件.小部件
检查

def check(event):
    event.widget.config(state="disabled", background="red")

最简单的修复方法是在函数中使用
事件小部件
检查

def check(event):
    event.widget.config(state="disabled", background="red")
函数
letter()
将每个按钮分配给同一个变量名,因此最后分配的按钮自然是唯一可以编辑的按钮,因为之前的所有分配都已被覆盖

我会使用
列表
来存储您的按钮,并根据索引引用它们。 在这种情况下,只要将lambda函数写入按钮的命令参数,就不需要绑定按钮

通过使用列表直接编辑按钮,我们还可以通过在相同的
检查
功能中引用按钮
['text']
来使用被选择的字母,然后在你的刽子手游戏中使用该字母

请注意,下面的代码大约比您的代码短20行,因为我们在循环中管理
bind
命令,而不是逐行管理。这是通过简单地迭代大写字母的字符串并使用
enumerate
获得列表的索引值来实现的,该索引值可以直接转换为用于存储按钮的列表的相同索引

import tkinter as tk


root = tk.Tk()
btn_list = []
root.title("HANGMAN")
root.geometry("800x800")


def check(ndex):
    # Index used to work with the button directly in list
    btn_list[ndex].config(state="disabled", background="red")
    # example to show we can get the letter from the buttons text field.
    print(btn_list[ndex]['text'])


def create_buttons():
    row = 0  # tracking the row for our grid statement
    col = 1  # tracking the column for our grid statement
    for ndex, char in enumerate('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
        btn_list.append(tk.Button(frame2, text=char, width="3", height="2", bg="lightblue",
                                  activebackground="lightblue", command=lambda ndex=ndex: check(ndex)))
        btn_list[-1].grid(row=row, column=col)
        # Simple math to manage button placement for row and column
        if col <= 12:
            col += 1
        else:
            col = 1
            row += 1


frame1 = tk.Frame(root, width="600", height="600")
frame2 = tk.Frame(root, bg="grey", width="200")
frame1.grid(row=0, sticky='e')
frame2.grid(row=1, sticky='e')
create_buttons()
root.resizable(False, False)
root.mainloop()
将tkinter作为tk导入
root=tk.tk()
btn_列表=[]
根标题(“刽子手”)
根几何(“800x800”)
def检查(ndex):
#用于直接在列表中使用按钮的索引
btn_列表[ndex].config(state=“disabled”,background=“red”)
#示例显示我们可以从按钮文本字段中获取字母。
打印(btn_列表[ndex]['text'])
def create_按钮():
row=0#跟踪网格语句的行
col=1#跟踪网格语句的列
对于ndex,枚举中的字符('abcdefghijklmnopqrstuvxyz'):
btn_list.append(tk.Button)(frame2,text=char,width=“3”,height=“2”,bg=“lightblue”,
activebackground=“lightblue”,command=lambda ndex=ndex:check(ndex)))
btn_列表[-1]。网格(行=行,列=列)
#管理行和列的按钮放置的简单数学
如果col您的函数
letter()
将每个按钮分配给相同的变量名,那么最后分配的按钮自然是唯一可以编辑的按钮,因为之前的所有分配都已被覆盖

我会使用
列表
来存储您的按钮,并根据索引引用它们。 在这种情况下,只要将lambda函数写入按钮的命令参数,就不需要绑定按钮

通过使用列表直接编辑按钮,我们还可以通过在相同的
检查
功能中引用按钮
['text']
来使用被选择的字母,然后在你的刽子手游戏中使用该字母

请注意,下面的代码大约比您的代码短20行,因为我们在循环中管理
bind
命令,而不是逐行管理。这是通过简单地迭代大写字母的字符串并使用
enumerate
获得列表的索引值来实现的,该索引值可以直接转换为用于存储按钮的列表的相同索引

import tkinter as tk


root = tk.Tk()
btn_list = []
root.title("HANGMAN")
root.geometry("800x800")


def check(ndex):
    # Index used to work with the button directly in list
    btn_list[ndex].config(state="disabled", background="red")
    # example to show we can get the letter from the buttons text field.
    print(btn_list[ndex]['text'])


def create_buttons():
    row = 0  # tracking the row for our grid statement
    col = 1  # tracking the column for our grid statement
    for ndex, char in enumerate('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
        btn_list.append(tk.Button(frame2, text=char, width="3", height="2", bg="lightblue",
                                  activebackground="lightblue", command=lambda ndex=ndex: check(ndex)))
        btn_list[-1].grid(row=row, column=col)
        # Simple math to manage button placement for row and column
        if col <= 12:
            col += 1
        else:
            col = 1
            row += 1


frame1 = tk.Frame(root, width="600", height="600")
frame2 = tk.Frame(root, bg="grey", width="200")
frame1.grid(row=0, sticky='e')
frame2.grid(row=1, sticky='e')
create_buttons()
root.resizable(False, False)
root.mainloop()
将tkinter作为tk导入
root=tk.tk()
btn_列表=[]
根标题(“刽子手”)
根几何(“800x800”)
def检查(ndex):
#用于直接在列表中使用按钮的索引
btn_列表[ndex].config(state=“disabled”,background=“red”)
#示例显示我们可以从按钮文本字段中获取字母。
打印(btn_列表[ndex]['text'])
def create_按钮():
row=0#跟踪网格语句的行
col=1#跟踪网格语句的列
对于ndex,枚举中的字符('abcdefghijklmnopqrstuvxyz'):
btn_list.append(tk.Button)(frame2,text=char,width=“3”,height=“2”,bg=“lightblue”,
activebackground=“lightblue”,command=lambda ndex=ndex:check(ndex)