3

whats the function for deleting selected notebook tab in tkinter? I couldn't find anything about this on the web?

This is the code that I wrote, i only need function:

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()

2 Answers 2

6

As rightly pointed by @Bryan Oakley, the above accepted answer doesn't actually "delete" the selected notebook, the object still exists but hidden from the view.

To actually delete the tab, call the .destroy() method on the child as below:

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

This technique worked for me. Here's a complete sample code to test it out:

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()
Sign up to request clarification or add additional context in comments.

Comments

5
def deletetab():
    tasktabs.forget(tasktabs.select())

deletes the currently selected tab.

5 Comments

Thanks my friend, please , if you can help me with this also: stackoverflow.com/questions/50199402/…
It's important to mention that forget doesn't delete the tab, it just removes it from view.
@Bryan: Is it possible to delete it. I tried with .destroy() but the object seems to still be there..
Yes, it's possible to delete it. Unfortunately, tasktabs.select() returns the name of the widget rather than the actual frame widget, and you need a reference to the widget to destroy it.
I was confused because I tried to destroy with the correct reference but when I printed the ref it still existed. But when I printed winfo_children() it was gone. Thanks anyway, and now I've found five ways of getting a widget reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.