Tk GUIs

Tk GUIs allow for customizing the HyperWorks Desktop interface and for creating new functionality. All of the standard widgets available in Tk, as well as additional widgets accessed through the HyperWorks GUI Toolkit (HWT) and the HyperWorks Automation Toolkit (HWAT), are available.

Widgets form the basis of creating Tk GUIs. Widgets are windows in the graphical user interface that have a certain appearance and functionality. Examples of widgets include entry boxes, buttons, frames, scrollbars, checkboxes, trees and menus. These widgets can then be organized in a hierarchical fashion to arrange them on the screen.

The size and location of the widgets is managed by a geometry manager. Tk provides the grid, pack and place geometry managers. The main caveat of the geometry managers is that a frame widget is used as a container for other widgets. Complex layouts are accomplished by creating frames within frames.

The following example of a Tk GUI creates a popup window, a listbox to select a component card image based on the current user profile, and an entry box for a component name:
set base .example1;
toplevel $base;

wm title $base "Example 1";
wm geometry $base 200x81;

#Create a master frame
set master_frame [frame $base.master_frame];
pack $master_frame -side top -anchor nw -padx 7 -pady 7 -expand 1 -fill both;

   #Create a frame for the spinbox
   set cardimage_frame [frame $master_frame.cardimage_frame];
   pack $cardimage_frame -side top -anchor nw;

      #Create the spinbox with a label
      set cardimage_label [label $cardimage_frame.cardimage_label \
         -width 19 \
         -anchor w \
         -text "Component card image:"];

      set cardimage_list [hm_getsolvercards components -byentitytype];
      set cardimage_list [lsort -dictionary $cardimage_list];
      set cardimage [lindex $cardimage_list 0];

      set cardimage_spinbox [spinbox $cardimage_frame.cardimage_spinbox \
         -width 8 \
         -values $cardimage_list];

      pack $cardimage_label -side left -anchor w;
      pack $cardimage_spinbox -side left -anchor nw -padx 2;

   #Create a frame for the label and entrybox
   set compname_frame [frame $master_frame.compname_frame];
   pack $compname_frame -side top -anchor nw -fill x -expand 1;

      #Create the entrybox with a label
      set compname "";
      set compname_label [label $compname_frame.compname_label \
         -width 19 \
         -anchor w \
         -text "Component name:"];

      set compname_entry [entry $compname_frame.compname_entry  \
         -text $compname \
         -textvariable compname];

      pack $compname_label -side left -anchor w -pady 4;
      pack $compname_entry -side left -anchor nw -pady 4 -padx 2 -fill x -expand 1;

   #Create a frame for the buttons
   set buttons_frame [frame $master_frame.buttons_frame];
   pack $buttons_frame -side top -anchor nw -expand 1 -fill both;

      set accept_button [button $buttons_frame.accept \
         -text "Accept" \
         -relief raised \
         -command "create_comp $cardimage $compname"];

      set cancel_button [button $buttons_frame.cancel \
         -text "Cancel" \
         -relief raised \
         -command "destroy $base"];

      pack $cancel_button -side right -anchor se;
      pack $accept_button -side right -anchor se -padx 4;

In this example, you need to create the procedure create_comp that is called by the "Accept" button for creating the component with the component name and card image passed as arguments.

Many widgets are also available via the HWT and HWAT commands for creating GUIs more efficiently and easily. These toolkits also add additional widgets or widget options that aren’t available as part of standard Tk. As an example, a similar Tk GUI can be created using several HWT commands:
package require hwt;

set base [::hwt::CreateWindow .example2 \
   -windowtitle "Example 2" \
   -geometry 200x110 \
   -acceptbutton "Accept" \
   -acceptfunc {::create_comp $::cardimage $::compname} \
   -noGeometrySaving \
   -propagate 1];

::hwt::PostWindow $base;

#Get the recess frame
set recess [::hwt::WindowRecess .example2];

#Create a master frame
set master_frame [frame $recess.master_frame];
pack $master_frame -side top -anchor nw -fill x;

   #Create a frame for the entry list
   set cardimage_frame [frame $master_frame.cardimage_frame];
   pack $cardimage_frame -side top -anchor nw;

      #Create the entry list with a label
      variable cardimage_list [hm_getsolvercards components -byentitytype];
      set cardimage_list [lsort -dictionary $cardimage_list];
      variable cardimage [lindex $cardimage_list 0];

      set cardimage_entry [::hwt::AddEntry $cardimage_frame.cardimage_entry \
         -label "Component card image:" \
         -labelwidth 19 \
         -entrywidth 8 \
         -listvar frompopdown notyping cardimage_list \
         -withoutpacking \
         -textvariable cardimage];

      pack $cardimage_entry -side top -anchor nw;

   #Create a frame for the label and entrybox
   set compname_frame [frame $master_frame.compname_frame];
   pack $compname_frame -side top -anchor nw -fill x;

      #Create the entrybox with a label
      variable compname "";
      set compname_entry [::hwt::AddEntry $compname_frame.compname_entry  \
         -label "Component name:" \
         -labelwidth 19 \
         -text $compname \
         -withoutpacking \
         -textvariable compname];

      ::hwt::AddPadding $compname_frame -side top height [hwt::DluHeight 4] \
         width [hwt::DluWidth 0]
      pack $compname_entry -side top -anchor nw -fill x;