python - 制作将自己定位在列表中的按钮

标签 python arrays python-3.x tkinter

我正在尝试创建可以自行删除的按钮。在下面的代码中,我在for循环中创建了一些按钮,将它们附加到列表中,然后将其网格网格。我能够删除和删除按钮列表的某个索引位置中的任何按钮,但需要弄清楚如何使每个按钮在按钮列表中找到自己的位置。

from tkinter import *
import random

class App(Tk):
    def __init__(self):
        Tk.__init__(self)

        self.totalButtons = random.randint(5, 25)

        # The buttons will be stored in a list
        self.buttons = []
        self.labels = []

        self.createButtons()

    def createButtons(self):
        for i in range(0, self.totalButtons):
            # Here I create a button and append it to the buttons list
            self.buttons.append(Button(self, text = i, command = self.removeButton))

            # Now I grid the last object created (the one we just created)
            self.buttons[-1].grid(row = i + 1, column = 1)

            # Same thing for the label
            self.labels.append(Label(self, text = i))
            self.labels[-1].grid(row = i + 1, column = 0)

    def removeButton(self):
        # When a button is clicked, buttonIndex should be able to find the index position of that button in self.buttons
        # For now I set it to 0, which will always remove the first (top) button
        indexPosition = 0

        # Takes the button (in this case, the first one) off the grid
        self.buttons[indexPosition].grid_forget()

        # Removes that button from self.buttons
        del self.buttons[indexPosition]

        # Same for the label
        self.labels[indexPosition].grid_forget()
        del self.labels[indexPosition]


def main():
    a = App()
    a.mainloop()

if __name__ == "__main__":
    main()

谢谢!

最佳答案

command = lambda idx=i: self.removeButton(idx)

这里的 lambda 是从范围中获取值 i 并将其分配给 idx 并传递到函数中。现在,此函数调用对于每个按钮都是唯一的,因此它们具有与其索引相对应的值。

for i, btn in enumerate(self.buttons):
    btn['command'] = lambda idx=i: self.removeButton(idx)

因为您要从列表中删除每个按钮,所以需要为命令参数分配一个新值,以正确反射(reflect)列表中现有按钮的新位置。

    def createButtons(self):
        for i in range(0, self.totalButtons):
            # Here I create a button and append it to the buttons list
            self.buttons.append(Button(self, text = i, command = lambda idx=i: self.removeButton(idx)))

            # Now I grid the last object created (the one we just created)
            self.buttons[-1].grid(row = i + 1, column = 1)

            # Same thing for the label
            self.labels.append(Label(self, text = i))
            self.labels[-1].grid(row = i + 1, column = 0)

    def removeButton(self, i):
        # Takes the button at index i off the grid
        self.buttons[i].grid_forget()

        # Removes that button from self.buttons
        del self.buttons[i]

        # Same for the label
        self.labels[i].grid_forget()
        del self.labels[i]

        # Assign new values for index position
        for new_i, btn in enumerate(self.buttons):
            btn['command'] = lambda idx=new_i: self.removeButton(idx)

关于python - 制作将自己定位在列表中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34136434/

相关文章:

python - 将数据框转换为 LaTeX

python - 从 Pandas 字典列表中删除多级列

python - 从浏览器运行 Python 应用程序

java - 无法更改数组的大小

python - multiprocessing.Pool.apply 和 multiprocessing.Pool.apply_async 的目的

python - 在 Python 中处理数据的首选方式是什么?

python - 我应该使用 tfidf 语料库还是仅使用语料库来使用 LDA 推断文档?

javascript - 如何通过Lodash将对象转换为排序数组

javascript - 避免插入重复的子数组

python - Python中使用GRIP处理图像的Segmentation Fault