package require Tk


ttk::button .b1 -text "create toplevel"
bind .b1 <ButtonPress-1> {
    createTL .tl
    break
}
ttk::button .b2 -text "RE - create toplevel"
bind .b2 <ButtonPress-1> {
    recreateTL .tl
    break
}
ttk::button .b3 -text "destroy and create toplevel"
bind .b3 <ButtonPress-1> {
    recreateTL2 .tl
    break
}
grid .b1 -sticky news -pady 5
grid .b2 -sticky news -pady 5
grid .b3 -sticky news -pady 5

if {[winfo exists .menuBar]} {
    destroy .menuBar
}
menu .menuBar -tearoff 0
menu .menuBar.f
.menuBar.f add command -label "print" -command {puts "first"}
.menuBar add cascade -menu .menuBar.f -label first
menu .menuBar.s
.menuBar.s add command -label "print" -command {puts "second"}
.menuBar add cascade -menu .menuBar.s -label second
. configure -menu .menuBar
        
proc killChildFrame {parent} {
    if {[winfo exists $parent]} {
        foreach w [winfo children $parent] {
            if {[winfo exists $w]} {
                destroy $w
            }
        }
    }
}

proc fill_tl {win} {
    frame $win.main

    for {set i 0} {$i < 10} {incr i} {
        frame $win.main.f$i -width [expr {200 - $i*10}] -height [expr {20 - $i*2}]  -bg gray[expr {$i*6+1}]
        grid $win.main.f$i  -sticky news -padx 2 -pady 2
    }

    grid $win.main  -sticky nsew -padx 2 -pady 2
    grid rowconfigure $win.main all -weight 1
    grid columnconfigure $win.main all -weight 1

}

proc createTL {win} {
    if {[winfo exists $win]} {
        destroy $win
        update idletasks
    }
    
    toplevel $win
    wm withdraw $win
    wm geometry $win =+220+0
    update idletasks

    $win configure -menu  .menuBar
    
    fill_tl $win
    
    wm deiconify $win
    update idletasks
}

proc recreateTL {win} {
    wm withdraw $win
    update idletasks

    killChildFrame $win

    $win configure -menu  .menuBar

    fill_tl $win

    wm deiconify $win
    update idletasks
}

proc recreateTL2 {win} {
    destroy $win
    update idletasks
    createTL $win
}
