Python 有没有办法在Tkinter中表示父帧?我能';由于函数写入错误,无法按名称引用帧
我有一个带有复选框和X按钮的框架。当我点击X按钮时,我想删除该帧。有没有办法表示父帧?我不能用它的变量名来引用它,因为我使用了一个函数,所以所有相关的帧都使用相同的名称 我的代码:Python 有没有办法在Tkinter中表示父帧?我能';由于函数写入错误,无法按名称引用帧,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我有一个带有复选框和X按钮的框架。当我点击X按钮时,我想删除该帧。有没有办法表示父帧?我不能用它的变量名来引用它,因为我使用了一个函数,所以所有相关的帧都使用相同的名称 我的代码: import tkinter master = tkinter.Tk() master.title("test1") master.geometry("300x300") checkboxArea = tkinter.Frame(master) checkboxArea.pack(fill=tkinter.X)
import tkinter
master = tkinter.Tk()
master.title("test1")
master.geometry("300x300")
checkboxArea = tkinter.Frame(master)
checkboxArea.pack(fill=tkinter.X)
inputStuff = tkinter.Frame(master)
def removeInputStuff():
inputStuff.pack_forget()
buttonAdd.pack()
buttonDone.remove()
buttonDone = tkinter.Button(inputStuff, text = "Close Input", command=removeInputStuff)
def createInputStuff():
paddingFrame = tkinter.Frame(inputStuff, height=5)
paddingFrame.pack(fill=tkinter.X)
buttonDone.pack()
inputStuff.pack()
buttonAdd.pack_forget()
buttonAdd = tkinter.Button(master, text="Add Item", command=createInputStuff)
buttonAdd.pack()
topInput = tkinter.Frame(inputStuff)
bottomInput = tkinter.Frame(inputStuff)
topInput.pack()
bottomInput.pack()
def drawCheckboxAndDeleteInputBoxAndPrompt():
nextToCheckbox = entry.get()
entry.delete(0,tkinter.END)
checkboxRow = tkinter.Frame(checkboxArea)
checkboxRow.pack(fill=tkinter.X)
checkbox1 = tkinter.Checkbutton(checkboxRow, text = nextToCheckbox)
checkbox1.pack(side=tkinter.LEFT)
deleteItem = tkinter.Button(checkboxRow, text = "x", bg="red", fg="white", activebackground="white", activeforeground="red")
deleteItem.pack(side=tkinter.RIGHT)
prompt = tkinter.Label(topInput, text="What do you want your checkbox to be for?")
prompt.pack()
entry = tkinter.Entry(bottomInput, bd=3)
entry.pack(side=tkinter.LEFT)
buttonConfirm = tkinter.Button(bottomInput, text="Confirm", command=drawCheckboxAndDeleteInputBoxAndPrompt)
buttonConfirm.pack(side=tkinter.LEFT)
master.mainloop()
我会分配一个函数来删除checkboxRow,但我认为这会删除我的所有行。我刚刚添加了
command=checkboxRow.destroy
,当deleteItem
按钮创建并运行时(它只删除预期的项,而不是全部)
因此DrawCheckBox和DeleteInputBox和Prompt
变为
def drawCheckboxAndDeleteInputBoxAndPrompt():
nextToCheckbox = entry.get()
entry.delete(0,tkinter.END)
checkboxRow = tkinter.Frame(checkboxArea)
checkboxRow.pack(fill=tkinter.X)
checkbox1 = tkinter.Checkbutton(checkboxRow, text = nextToCheckbox)
checkbox1.pack(side=tkinter.LEFT)
deleteItem = tkinter.Button(checkboxRow, text = "x", bg="red", fg="white",
activebackground="white", activeforeground="red",
command=checkboxRow.destroy)
deleteItem.pack(side=tkinter.RIGHT)