slpanel megawidget

Tcl/Tk megawidget that provides animated, collapsible side panels. Source at https://github.com/ch32n/slpanel

Command Reference

slpanel::create pathName ?options?

Creates sliding panel megawidget

Widget Commands

pathName add PanelName ?options?

Adds new panel with name PanelName. PanelName is a unique string to identify the panel.

Returns the path to the panel's content frame, where you should pack your widgets.

Configuration Options:

  • -anchor (e|w): (Default: e) The side the panel slides from.
  • -open (float|integer): (Default: 0.5) The width of the open panel. A value between 0.0 and 1.0 is a percentage of the main area; a value > 1 is a fixed pixel size.
  • -close (float|integer): (Default: 0) The width of the closed panel, allowing a small part to remain visible. Sizing rules are the same as for -open.
  • -speed (integer): (Default: 70) A parameter controlling the animation speed. Higher is faster.

pathName remove PanelName

Destroys a panel and its content.

pathName itemconfigure PanelName ?options? ?value?

Configures an existing panel. If called with no options, returns a dictionary of all options. If called with a single option, returns its value.

pathName itemcget PanelName option

Returns the value of a single configuration option for the specified panel.

pathName openpanel PanelName

Starts the animation to open a panel.

pathName closepanel PanelName

Starts the animation to close a panel.

pathName togglepanel PanelName

Opens a closed panel or closes an open one.

pathName closeopened

Closes all currently open panels.

pathName getpanels

Returns a list of all panel names.

pathName getpanelstate PanelName

Returns the current state of a panel.

Values:

  • 1: Open
  • 0: closed

pathName getmainframe

Returns the path to the central content frame.

pathName getpanelframe PanelName

Returns the path to the content frame for a specific panel.

Virtual Events

slpanel generates virtual events on the panel's content frame to allow you to react to state changes.

<<slChange>>: Generated when an animation begins.

<<slChangeDone>>: Generated when an animation is complete.

The new state (0 or 1) is passed in the event's -data field, which can be accessed with the %d substitution.

Example:

set panelFrame [$panels getpanelframe myPanel]
bind $panelFrame <<slChangeDone>> {
    if {%d == 1} {
        puts "myPanel has finished opening!"
    }
}

 Example

slpanel-image1

package require slpanel

set buttonFrame   [ttk::frame .bf]
set toggle1 [ttk::button $buttonFrame.b1 -text "toggle 1"]
set toggle2 [ttk::button $buttonFrame.b2 -text "toggle 2"]
set close   [ttk::button $buttonFrame.b3 -text "close all"]

grid $buttonFrame -row 0 -column 0 -sticky nsw
grid $toggle1     -row 0 -column 0 -sticky nw
grid $toggle2     -row 1 -column 0 -sticky nw
grid $close       -row 2 -column 0 -sticky nw

# create sliding panel container
set slPanel [slpanel::create .slPanel]
grid $slPanel -row 0 -column 1 -sticky nesw

grid rowconfigure     . 0 -weight 1
grid columnconfigure  . 1 -weight 1

# get sliding panel main frame
set mainFrame  [$slPanel getmainframe]
grid rowconfigure     $mainFrame 0 -weight 1
grid columnconfigure  $mainFrame 0 -weight 1

# create canvas on sliding panel main frame
set maincFcanv [canvas $mainFrame.mCanvas -background white]
grid $maincFcanv -row 0 -column 0 -sticky nesw


# add 2 sliding panels
set slFrame1 [$slPanel add sl1 -speed 130 -close 0.2 -anchor e -open 0.5]
set slFrame2 [$slPanel add sl2 -speed 10  -anchor e -open [winfo pixels . 7c] ]

# bind sliding panel frames virtual events on state change
foreach slFrame [list $slFrame1 $slFrame2] {
        bind $slFrame <<slChange>>     [list puts "Started state change on slFrame: $slFrame new state: %d"]        
        bind $slFrame <<slChangeDone>> [list puts "State change done on slFrame: $slFrame new state: %d"]        
}

#create widgets in sliding panels
set maincFcanv1 [canvas $slFrame1.slCanvas1 -background red]

grid $maincFcanv1 -row 0  -column 0 -sticky nesw
grid rowconfigure    $slFrame1 0 -weight 1
grid columnconfigure $slFrame1 0 -weight 1


set maincFcanv2 [canvas $slFrame2.slCanvas2 -background green]

grid $maincFcanv2 -row 0  -column 0 -sticky nesw
grid rowconfigure    $slFrame2 0 -weight 1
grid columnconfigure $slFrame2 0 -weight 1

# close sliding panels when button1 click on main frame
bind $maincFcanv <Button-1> [list $slPanel closeopened]

# configure buttons
$toggle1 configure -command [list $slPanel togglepanel sl1]
$toggle2 configure -command [list $slPanel togglepanel sl2]
$close   configure -command [list $slPanel closeopened]