KWWidgets/Examples/First Look

From KitwarePublic
Jump to: navigation, search

Object-Oriented Library

As mentioned in the overview section, KWWidgets is an object-oriented C++ layer on top of the Tcl/Tk UI toolkit. The following code snippet demonstrates how to create a checkbutton inside a toplevel window.

C++
vtkKWApplication *app = vtkKWApplication::New(); Create an application object

vtkKWTopLevel *top = vtkKWTopLevel::New();
top->SetApplication(app);
top->Create();

Create a toplevel window

vtkKWCheckButton *cb = vtkKWCheckButton::New();
cb->SetParent(top);
cb->Create();

Create a checkbutton, parent it to the toplevel
cb->SetText("A checkbutton"); Set the checkbutton label
cb->Deselect(); Deselect the checkbutton
app->Script("pack %s", cb->GetWidgetName()); Pack the checkbutton inside its parent. We call Tcl directly, but this will change soon in favor of a C++ layout engine
A checkbutton

Fast-Prototyping and Scripting

Just as VTK, KWWidgets is wrapped automatically into a Tcl package and/or a Python module at compile-time, and can therefore be used directly from Tcl/Tk or Python, allowing you to fast-prototype applications, create small KWWidgets scripts that can be used from a Tcl/Tk or Python application, write testing or demo scripts. Tcl and Python scripts are interpreted directly, without the need to compile or link any single line of code. The following code snippets demonstrate how to create a checkbutton inside a toplevel window, and compares both the Tcl, Python and C++ approaches. A few details have been left out, but you can get a feeling of the syntaxes and notice how close they are.

A checkbutton
C++
vtkKWApplication *app = vtkKWApplication::New(); Create an application object

vtkKWTopLevel *top = vtkKWTopLevel::New();
top->SetApplication(app);
top->Create();

Create a toplevel window

vtkKWCheckButton *cb = vtkKWCheckButton::New();
cb->SetParent(top);
cb->Create();

Create a checkbutton, parent it to the toplevel
cb->SetText("A checkbutton"); Set the checkbutton label
cb->Deselect(); Deselect the checkbutton
app->Script("pack %s", cb->GetWidgetName()); Pack the checkbutton inside its parent. We call Tcl directly, but this will change soon in favor of a C++ layout engine


Tcl
package require kwwidgets Load the KWWidgets package in Tcl
set app [vtkKWApplication New] Create an application object

set top [vtkKWTopLevel New]
$top SetApplication $app
$top Create

Create a toplevel window

set cb [vtkKWCheckButton New]
$cb SetParent $top
$cb Create

Create a checkbutton, parent it to the toplevel
$cb SetText "A checkbutton" Set the checkbutton label
$cb Deselect Deselect the checkbutton
pack [$cb GetWidgetName] Pack the checkbutton inside its parent.


Python
from kwwidgets import * Load the KWWidgets module in Python
app = vtkKWApplication() Create an application object

top = vtkKWTopLevel()
top.SetApplication(app)
top.Create()

Create a toplevel window

cb = vtkKWCheckButton()
cb.SetParent(top)
cb.Create()

Create a checkbutton, parent it to the toplevel
cb.SetText("A checkbutton") Set the checkbutton label
cb.Deselect() Deselect the checkbutton
app.Script("pack %s", cb.GetWidgetName()) Pack the checkbutton inside its parent. We call Tcl directly, but this will change soon in favor of a C++ layout engine

Interacting With Tcl/Tk

As mentionned before, the core widgets in KWWidgets are thin C++ wrapper around the corresponding Tcl/TK widgets. If you are familiar with Tcl/Tk, the following code snippet demonstrates how to create a checkbutton inside a toplevel window, and compares both the native Tk and the C++ approaches side by side.

Tcl/Tk C++
vtkKWApplication *app = vtkKWApplication::New(); Create an application object
toplevel .top

vtkKWTopLevel *top = vtkKWTopLevel::New();
top->SetApplication(app);
top->Create();

Create a toplevel window
checkbutton .top.cb

vtkKWCheckButton *cb = vtkKWCheckButton::New();
cb->SetParent(top);
cb->Create();

Create a checkbutton,
parent it to the toplevel
.cb config -text "A checkbutton" cb->SetText("A checkbutton"); Set the checkbutton label
.cb deselect cb->Deselect(); Deselect the checkbutton
pack .cb app->Script("pack %s", cb->GetWidgetName()); Pack the checkbutton inside its parent


The KWWidgets C++ library can be used directly from Tcl, but the opposite is also true, KWWidgets can interact and co-exist with Tcl/Tk directly from C++, allowing you to load or execute Tcl modules straight from a C++ application. The following code snippet demonstrates how the Script() method is used to execute Tcl/Tk code and create a native Tk frame, which is then wrapped around a C++ KWWidgets object and integrated directly into the KWWidgets framework.

C++
vtkKWApplication *app = vtkKWApplication::New(); Create an application object

app->Script("frame .myframe");
app->Script("pack .myframe");

Invoke Tcl/Tk to create a native Tk frame

vtkKWWidget *myframe = vtkKWWidget::New();
myframe->SetWidgetName(".myframe");
myframe->SetApplication(app);
myframe->Create();
myframe->SetBackgroundColor(0.2, 0.5, 0.8);

Create a vtkKWWidget object to wrap around the native Tk frame, then use the C++ API to change the native frame color

vtkKWPushButton *pb = vtkKWPushButton::New();
pb->SetParent(myframe);
pb->Create();
pb->SetCommand(NULL, ".myframe config -bg #223344");

Create a button object, child of the frame, set its C++ callback to invoke a Tcl command that changes the color of the frame
app->Script("pack %s", cb->GetWidgetName()); Pack the button inside its frame



KWWidgets: [Welcome | Site Map]