KWWidgets/Examples/First Look
From KitwarePublic
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(); | Create a toplevel window | |
|
vtkKWCheckButton *cb = vtkKWCheckButton::New(); | 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 |
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.
| C++ | ||
|---|---|---|
| vtkKWApplication *app = vtkKWApplication::New(); | Create an application object | |
|
vtkKWTopLevel *top = vtkKWTopLevel::New(); | Create a toplevel window | |
|
vtkKWCheckButton *cb = vtkKWCheckButton::New(); | 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] | Create a toplevel window | |
|
set cb [vtkKWCheckButton New] | 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() | Create a toplevel window | |
|
cb = vtkKWCheckButton() | 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(); | Create a toplevel window | ||
| checkbutton .top.cb |
vtkKWCheckButton *cb = vtkKWCheckButton::New(); | 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"); | Invoke Tcl/Tk to create a native Tk frame | |
|
vtkKWWidget *myframe = vtkKWWidget::New(); | 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(); | 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 |
| |


