Your First Widget
First question: what the heck is a widget?!? It's simply a component of your GUI. I didn't make it up, honest. Buttons, menubars, boxes, these are all widgets. Oooh, so we're finally going to do something fun? Yes! The extensions to Tcl that make up the Tk package are what let the computer understand your commands about widgets. What does this mean? If you try to use a widget command in tclsh, it won't work because it only understands pure Tcl. Since Tcl and Tk are packaged together, you won't really have to worry about any of this when you use wish. You'll just notice that the Tk commands effect things in the wish window, while Tcl commands don't (unless they feed into a widget!).
Enough of these details, let's make a widget! Widgets are not one line commands, you have to first tell wish what you want the widget to look like and do, and then you have to tell it to put it on the screen. The syntax for the first part is: widget_type widget_name option1 option2... You can also modify a widget after you've created it, but more on that later. Alright, let's make a button!
button .firstWidget -text "Hello World!"
You should see a ".firstWidget" appear, which means that no errors were made. You may also notice that nothing in your wish window changed. That's because you have to use something called the Packer Geometry Manager. This is easier than it sounds, just type pack .firstWidget
Your console should show:
% button .firstWidget -text "Hello World!"
% pack .firstWidget
You'll notice that there is no acknowledging line after you type the pack command. You will also notice that you've made a button!!! (It's in your, now much smaller, wish window) If you're a big geek like me, you will be taking a few moments to yank any unfortunately nearby friends/associates/co-workers over to your desk and shout and point a lot while they confusedly wonder, "what's the deal with a button that says 'Hello World' and doesn't do anything?" At this point, just kick them out and dance around on your own for a bit. Isn't this cool? True, it doesn't do a blasted thing, but in two measly lines of code you've made a button.
Ok, it's a button. That rocks, but it's lacking something. Style? Flair? Some might say pizzazz. Try this out: .firstWidget configure -background blue Look at that! Try it again, this time using foreground instead of background, and pick another color (I used white). Your wish window should look something like this:
Man but that is cool. Ok, it's a widget. It doesn't do anything, but it sure is fine looking. Hey, in the GUI world, isn't that all that matters? Yeah, Microsoft-bashing aside (I will not give in to temptation...) you have every right to be excited, hooked, and wanting more. Too bad. Now that I've got you here, we're going to go back to pure Tcl for a bit and really figure out its power. We'll come back to widgets later (trust me! They're the coolest part), but first we need to learn just what Tcl can do.
Copyright 1998 by Chris Palmer
Mail all comments to: Ardenstone@Ardenstone.com
Visit the rest of my Senior Seminar
or my homepage.