Write a New Custom Widget

Steps and examples for writing a custom widget using the toolkit.

  1. Create a class.
    1. Create XYZentry inheriting from hwtk::interface::HWIWidget
      itcl::class XYZentry {
          inherit hwtk::interface::HWIWidget;
      
          itk_option define -orient orient Orient "horizontal";
      
          constructor {args} {};
          destructor} {};
      
          public method get {args};
      }
  2. Writing the constructor:
    itcl::body XYZentry:constructor {args} {
        itk_component add hull {ttk::frame $itk_interior } { }
        itk_component add x {hwtk::label $itk_interior.__x -text "X:"} { }
        itk_component add xe {hwtk::entry $itk_interior.__xe -inputtype double} { }
        itk_component add y {hwtk::label $itk_interior.__y -text "Y:"} { }
        itk_component add ye {hwtk::entry $itk_interior.__ye -inputtype double} { }
        itk_component add z {hwtk::label $itk_interior.__z -text "Z:"
        itk_component add ze {hwtk::entry $itk_interior.__ze -inputtype double
    
    hwtk_setdefaultbindings;
    eval itk_initialize ;
    }
  3. Writing the Option Config body:
    itcl::configbody XYZentry::orient {
         switch -glob --itck_option(-orient)
              h* {
                   pack configure $itk_component(x) $itk_component(xe) -side left
                   pack configure $itk_component(y) $itk_component(ye) -side left
                   pack configure $itk_component(z) $itk_component(ze) -side left
              )
              v* {
                   pack configure $itk_component(x) $itk_component(xe) -side top
                   pack configure $itk_component(y) $itk_component(ye) -side top
                   pack configure $itk_component(z) $itk_component(ze) -side top
              )
         }
    }
  4. Writing with the Public method (get)
    itcl::body XYZentry::get {args} {
         if {[llength [info level 0]]} {
              return [list [ $itk_component(xe) [$itk_component(ye) [$itk_component(ze) ]
         }
         if {[llength [info level 0]]> 2} {
              return -code error "wrong args # should be $itk_component (hull) get ?X? ?Y? ?Z?"
         }
         switch -- $args{
              X {return [$itk_component(xe) get]}
              Y {return [$itk_component(ye) get]}
              Z {return [$itk_component(ze) get]}
         }
    }
Simple custom widget
toplevel .t
pack [XYZentry .t.e]
Accessing the public method
% .t.e get X
12.323
% .t.e get Y
22.333
% .t.e get Z
45.123
% .t.e get
12.323 22.333 45.123