ttk:notebook help

Can anybody tell me how to delete the tabs created by ttk:notebook? I can create as many as tabs I want, however, I do not know how to delete/close it, and I'd like to run some command when closing it for example, saving files. Thanks for your help.

RLE (2011-03-17): man n ttk_notebook:

 pathname forget tabid
   Removes the tab specified by tabid, unmaps and unmanages the
   associated window.

Looking in the documentation as a first step is always a good idea.

hshang - 2011-03-17 18:19:48

thanks. Guess my question is not clear. I want to have a "X" button by tab label, like the tab frame in a terminal; so that it tab can be closed when "X" button is clicked. However, I could not find a way to add the button.

RLE - (2011-03-17): Take a look at the ttk::notebook page, in the section on "Tab list scrolling". It is not directly what you ask for, but it is going in the same direction and you might be able to derive some help from how that works.


hshang - 2011-03-18 11:24:37

Thank you. I know that I can add a button in the top of the tab frame, however, it does not look nice. I'd like it to be in same horizontal level of the "text" of the tab, for example,

pack [ttk:notebook .nb]
.nb add [frame .nb.tab1] -text "tab1"

How can I add a button besides the label "tab1"? What widget does "tab1" belongs to? I can not find it out anywhere, if I know the parent widget of "tab1", I shall be able to add a button there. Otherwise, I strongly suggest that ttk:notebook add a close button "x" by the text, and provide a user to be able to customerize the "close" command.

Thanks.


hshang - 2011-03-18 12:40:37

right now, I added a close "button" in the top frame (above the notebook) to close selected tabs, and it works as I need. Can you tell me what image is for the "close" button? which has "red background, and white "x"" on it. Thanks. I used "x" text for it right now.

MG offers this. On Windows it uses native close/restore buttons, on other platforms it uses text 'X' and '<>' labels that could easily be replaced with images. (Change WINDOW 20 to WINDOW 19 to get a red/white close button instead.)

pack [ttk::notebook .foo]
.foo add [text .foo.t1] -text "Foo"
.foo add [text .foo.t2] -text "Bar"
.foo add [text .foo.t3] -text "Baz"

set hide(.foo) [list]

if { $tcl_platform(platform) eq "windows" && \
     ![catch {ttk::style element create close vsapi WINDOW 20 {disabled 4 pressed 3 active 2 {} 1}}] } {
     ttk::style layout CloseButton {CloseButton.close -sticky news}
     ttk::button .close -style CloseButton
   } else {
     ttk::button .close -text X -width 1
   }
   
if { $tcl_platform(platform) eq "windows" && \
     ![catch {ttk::style element create restore vsapi WINDOW 22 {disabled 4 pressed 3 active 2 {} 1}}] } {
     ttk::style layout RestoreButton {RestoreButton.restore -sticky news}
     ttk::button .rest -style RestoreButton
   } else {
     ttk::button .rest -text <> -width 2
   }

.close config -command [list closeTab .foo .close .rest]
.rest config -command [list restoreTab .foo .close .rest]
.rest state disabled
place .close -in .foo -relx 1 -rely 0 -x -3 -y 3 -anchor ne

place .rest -in .foo -relx 1 -rely 0 -x [expr {-8 - [winfo reqwidth .rest]}] -y 3 -anchor ne


proc closeTab {nb close restore} {
  
  if { [catch {$nb index current} curr] || $curr eq ""  } {
    $close state disabled
  } else {
    $nb hide $curr
    lappend ::hide($nb) $curr
    $restore state !disabled
    if { [catch {$nb index current} curr] || $curr eq ""  } {
         $close state disabled
       } else {
         $close state !disabled
       }
    
  }
}

proc restoreTab {nb close restore} {
  global hide;
  
  if { ![info exists hide($nb)] } {
       return;
     }
     
  if { ![llength $hide($nb)] } {
       return;
     }
     
  set last [lindex $hide($nb) end]
  set hide($nb) [lrange $hide($nb) 0 end-1]
  $nb add [set path [lindex [$nb tabs] $last]]
  $nb select $path
  $close state !disabled
  if { [llength $hide($nb)] } {
       $restore state !disabled
     } else {
       $restore state disabled
     }
}