【问题标题】:Python Tkinter Removing a Frame on button clickPython Tkinter 在按钮单击时删除框架
【发布时间】:2021-05-17 00:40:03
【问题描述】:

我正在尝试制作一个具有基于单击按钮添加和删除框架的功能的应用程序。目前,我正在尝试让它从根目录中删除最后添加的帧,并将添加功能以指定稍后要删除的帧。

当我创建框架时,我将它添加到这样的对象列表中:

def AddFrame(self):
    newFrame= customFrame(len(self.frameList))
    self.frameList.append(newFrame)
    newFrame.pack()

当我试图通过引用删除它时,我正在使用 pop 和 pack_forget 将其从列表中删除并从 GUI 中删除。我的基本功能是:

def RemoveLastFrame(self):
    if(len(self.frameList)>0):
        self.frameList.pop().pack_forget()

我通过在我在 init 方法中创建的指定帧上测试它来尝试 pack_forget。我不确定这个问题是否来自我试图在框架对象列表上执行此操作,或者是否是由于框架对象是 tk.Frame 的子对象。这是完整的子类:

    class customFrame(tk.Frame):
        def __init__(self, index):
            super(customFrame, self).__init__()
            self.index = index
            self.buttons= []
            self.addButton = tk.Button(command=self.AddButton,text='Add Button to Frame')
            self.addButton.pack()
    
        def AddButton(self):
            newButton = customButton(len(self.buttons))
            newButton.config(text=('Button '+str(newButton.index)))
            self.buttons.append(newButton)
            newButton.pack()
    
    
class customButton(tk.Button):
    def __init__(self,index):
        super(customButton, self).__init__()
        self.index = index

【问题讨论】:

    标签: python tkinter button frame


    【解决方案1】:

    我是个白痴。它正在工作,但它并没有像我预期的那样解开框架的孩子。一旦我改变了它,它就完美地工作了

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多