无法通过单击更改按钮颜色,每个按钮都是由函数创建的

问题描述 投票:0回答:2

我们想在单击时更改按钮的颜色,但是问题在于,它仅更改了最后一个按钮的颜色,因为我们都是通过函数创建它们的。问题是是否有解决此问题的选项?

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

我们无法解决此问题,因为网络上没有此类信息。如果您能帮助我们,我们将不胜感激。

python function tkinter
2个回答
0
投票

最简单的解决方法是在函数event.widget中使用check

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

0
投票

您的循环将每个按钮分配给相同的变量名,因此自然而然,最后一个分配的按钮将是唯一可以编辑的按钮,因为先前的所有分配都已被覆盖。

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

通过使用列表直接编辑按钮,我们还可以通过在相同的['text']功能中引用按钮check来处理所选字母,然后为您的子手游戏处理该字母。

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

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.