5

I want to make a frame that can be hidden and shown alternatively. The problem is Tk does not provide any hide/unpack command. I use vtcl and there is an option "Window hode" which only hides the window at top level. Now I want to hide a frame and later show the same frame again. It can be thought of as unpacking one frame and showing the other. My code can be like this:

proc show1hide2 { } {
    global i top
    if {$i == 1} {
        unpack $top.frame1
        pack $top.frame2
        set i 0
    } else {
        unpack $top.frame2
        pack $top.frame1
        set i 1
    }
}

In this procedure, $top.frame1 and $top.frame2 were previously filled and value of $i is toggled hence $top.frame1 and $top.frame2 are shown alternatively when this proc is called. All, I want to know is that does there exists and command like unpack which can help me do this? By the way, unpack here is just an idea.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
user954134
  • 129
  • 2
  • 8
  • There seems to be another solution using "stacked frames" and the `raise` command (I did not try it yet). The advantage could be that the window will not shrink or grow (resize) if the two frames have a different size and a layout manager is used (e. g. `grid´): http://stackoverflow.com/questions/19404439/python-tkinter-how-to-hide-the-ui – R Yoda Jul 09 '16 at 15:06

1 Answers1

9

I think that the pack forget command might be what you are looking for:

proc toggle {} {
    global state
    if {$state == 1} {
        pack forget .r
        pack .g   -side bottom -fill x
        set state 0
    } else {
        pack forget .g
        pack .r   -side bottom -fill x

        set state 1
    }
}

set state 1

# Make the widgets
label .r -text "Red Widget"    -bg red
label .g -text "Green Widget" -bg green
button .tog -text "Toggle" -command toggle
# Lay them out
pack .tog
pack .r   -side bottom -fill x
Jackson
  • 5,627
  • 2
  • 29
  • 48
  • +1: You can use `pack info` to get the information to save about the packed window between calls so that the restore works properly. Alternatives are to use `grid` which has `grid remove` as well as `grid forget`, and I think you can configure the `ttk::notebook` widget to not have tabs with a suitable style (which has some advantages with widget layout). Alas, style hacking remains Deep Voodoo to a far greater extent than it should. :-( – Donal Fellows Oct 18 '11 at 09:11
  • Great code! I just wanted to know whether the windows shrinks and grows between the two `pack` commands (or adjusts the size only after the second pack as needed). Result: No shrink and grow-again, just one window size adjustment after the command handler "toggle" has finished. I have tested this by inserting `after 1000` between the two pack commands (to wait one second). My interpretation: Tcl/Tk is single threaded and must wait until the end of the function execution. – R Yoda Jan 08 '16 at 16:45