Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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_Loops_Tkinter_Tkinter Button - Fatal编程技术网

Python 是否有一种方法可以在单击按钮时更改迭代创建的按钮的图像?

Python 是否有一种方法可以在单击按钮时更改迭代创建的按钮的图像?,python,python-3.x,loops,tkinter,tkinter-button,Python,Python 3.x,Loops,Tkinter,Tkinter Button,按钮是在一个更大的循环中创建的,循环通过i然后是j,代码如下: btn[i][j] = Button(lbl_frm, width=24, height=24, image=unchecked_img, command=lamda:change_btn_img(btn[i][j]),relief=SOLID) global state state = &

按钮是在一个更大的循环中创建的,循环通过i然后是j,代码如下:

btn[i][j] = Button(lbl_frm, width=24, height=24, image=unchecked_img,
                                       command=lamda:change_btn_img(btn[i][j]),relief=SOLID)

                global state
                state = "unchecked"

                btn[i][j].place(relx=.5, rely=.5, anchor='c')
具有更改按钮配置的功能:

def change_btn_img(btn):
    global state
    if state == "checked":
        btn.configure(image=unchecked_img)
        state = "unchecked"

    elif state == "unchecked":
        btn.configure(image=checked_img)
        state = "checked"
然而,这并不像我点击任何按钮那样工作,它只会改变btn[I][j]的图像,其中I和j是在循环的最后一次迭代中达到的值。
按钮用于形成网格,在这种情况下,单击任何按钮都会更改最后一行中的最后一个元素。有没有办法使创建按钮时声明命令时使用的i和j固定到该特定按钮?

这是
循环中
lambda
的常见问题

您必须将
i
j
分配给
lambda
(即
x
y
)中的参数,它将从
i
j
复制值,而不是使用对
i
j

 command=lamda x=i, y=j:change_btn_img(btn[x][y]) 

顺便说一句:

在Python中,您可以在类中创建自己的atribute,因此您可能应该在
按钮中保留
状态

 btn[i][j].state = "unchecked"
然后,您不需要
全局
,每个按钮都有自己的
状态


或者,您应该创建自己的
小部件
——类似这样:

class MyButton(tkinter.Button):

    def __init__(self, parent, *args, **kwars):
        super().__init__(parent, width=24, height=24, image=unchecked_img, command=self.change, relief=tkitner.SOLID)
        self.state = "unchecked"
        
    def change(self):
        if self.state == "checked":
            self.configure(image=unchecked_img)
            self.state = "unchecked"
        elif self.state == "unchecked":
            self.configure(image=checked_img)
            self.state = "checked"
            
以后你可以简单地使用

btn[i][j] = MyButton(lbl_frm)

btn[i][j].place(relx=.5, rely=.5, anchor='c')

您好,很有趣,也许这是lambda在循环中的常见问题:
command=lamda x=i,y=j:change\u btn\u img(btn[x][y])
现在工作得很好,非常感谢。谢谢你的建议,我现在已经实现了一个按钮类。你可以将我的答案标记为已接受,几分钟后你可以投票表决。