在tkinter中删除所选笔记本标签的功能是什么? 我在网上找不到任何相关内容?
这是我写的代码,我只需要函数:
from tkinter import *
from tkinter import ttk
import math
import sys
myApp = Tk()
myApp.title(" Program ")
myApp.geometry("1000x1200")
tasktabs=ttk.Notebook(myApp)
TabOne=ttk.Frame(tasktabs)
tasktabs.add(TabOne,text="Tab One")
TabOne=ttk.Frame(tasktabs)
tasktabs.add(TabOne,text="Tab Two")
def deletetab():
# whats the function for deleting tab?
pass
DelButton=Button(myApp,text=' Delete ', command=deletetab)
DelButton.grid(row=0,column=3, sticky="W")
tasktabs.grid(row=0,column=0,sticky="W")
myApp.mainloop()
答案 0 :(得分:2)
正如@Bryan Oakley正确指出的那样,上面接受的答案实际上并未“删除”所选的笔记本,该对象仍然存在,但从视图中隐藏了。
要实际删除选项卡,请在子级上调用 .destroy()方法,如下所示:
def deletetab():
for item in tasktabs.winfo_children():
if str(item)==tasktabs.select():
item.destroy()
return #Necessary to break or for loop can destroy all the tabs when first tab is deleted
这项技术对我有用。这是一个完整的示例代码,可以对其进行测试:
from tkinter import *
from tkinter import ttk
def deletetab():
for item in nb.winfo_children():
if str(item) == (nb.select()):
item.destroy()
return #Necessary to break or for loop can destroy all the tabs when first tab is deleted
root = Tk()
button = ttk.Button(root,text='Delete Tab', command=deletetab)
button.pack()
nb = ttk.Notebook(root)
nb.pack()
f1 = ttk.Frame(nb)
f2 = ttk.Frame(nb)
f3 = ttk.Frame(nb)
f4 = ttk.Frame(nb)
f5 = ttk.Frame(nb)
nb.add(f1, text='FRAME_1')
nb.add(f2, text='FRAME_2')
nb.add(f3, text='FRAME_3')
nb.add(f4, text='FRAME_4')
nb.add(f5, text='FRAME_5')
root.mainloop()
答案 1 :(得分:1)
def deletetab():
tasktabs.forget(tasktabs.select())
删除当前选中的标签。