【发布时间】: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