making your own geometry manager

You can pack widgets, grid them, place them, or use the canvas or text widget to manage them.

There are explicitely 3 geometry managers: pack, grid, place.

There are two special ones that aren't usually thought of as being useful for managing widgets, but have a lot to offer: canvas, text.

What inspired me: I have been quite accustomed to the wonderful part of the web browsing experience where I can resize fonts with a Control-MouseWheel, fonts increasing and decreasing instantly giving my system fantastic feedback making me feel at ease and relaxed as my eyes relax as fonts and GUI elements grow large. And then grow small again, or smallify, so that I can see the entirety in its entirety, at a glance.

Grid has its own mini language, a shorthand language, which unfortunately I am not that jazzed about as it feels esoteric.

Placing widgets explicitely is wonderful for pixel precision and total control, but I feel that in 2011 it's quite necessary to have a scrollable frame, and to be able to scale widgets, especially in regards to font size.

So many of us have fought with pack.

Of course grid and pack are both very nice (especially when we compare with other languages/systems) but perhaps we can improve, at least so that we can have widgets many of them on a single toplevel, and we can scroll around, so the widgets are not limited to the size of the containing 'master' frame.

In other words, think of a webpage, or 'webapp', where we are used to the idea of scrolling GUI elements.

In Tcl/Tk we call this a scrollable frame, and a while back (10 years+) I used to use bwidgets for this. Now I feel comfortable enough to just use the canvas and a few tiny bindings and have the same sort of functionality, with more extensibility.

There is also something I haven't explored which is using the text widget as a container widget. Right now I focus on using the canvas widget for this, because so far it can do a heck of a lot, and feels very flexible. If I was mixing mostly text and peppering it with floating widgets then I think I would explore using the text widget to do geometry management more.

I have no real experience with complex widget layout inside of a text widget.

Widgets in a canvas are great, a few niggles, you have to make sure the managed widgets are a child of the canvas so they scroll properly, you have to set the canvas bounding box, and you have to give the x,y position to the canvas for the widget and its width and height.

I let the managed widgets be each packed in a frame, and have query the frame for its requested width and height. Then I let the canvas use this information to layout these items.

When the canvas, now our widget container, is resized, we catch that event by binding to <Configure>, and then update the positions of the managed widgets, as well resetting our bounding box which signals any scrollbars we have on our container canvas to also change shape.

An idea is to basically mimic a web browser with css -- not sticking to css, just the idea of flowable layout in a viewport, I'm used to how the GUI works in a browser, flowable widgets nice and scrollable all around and the ability to increase font sizes for all gui elements with a control-mousewheel. I haven't got this far, but it's an idea percolating at the back of my mind.

Using place i dont know how to make the frame scrollable, so the canvas it is. It works for a basic version (font changes work, on master resize the childs resize to fill width, floating layout where widgets get a percentage of the width not implemented).

Dynamically changing the font on the managed widgets feels so good, it feels so flexible. And it's super on the eyes.

More pages and ideas for widget layout aka geometry management:

Scrolled frame, canvas, auto scrollbars

more ideas on widget layout

Fabricio Rocha - 26 Aug 2011 - Why? The text was rather confusing indeed, but the concept is interesting. Some days ago I was thinking about how could Tk canvas be used for creating interfaces in Hypercard style -- having one or more "background" images, such as a school notebook or an airplane cockpit, over which one could create a "layer" of clickable areas (buttons), selectable/clickable text (listboxes and alikes) and "fields" for user keyboard input. I am not sure this could be called a new "geometry manager", but it might be somehow used like one...

Ro: Interesting idea Fabricio. I have never used Hypercard but my cousin described it to me and remembers it fondly. What you're describing sounds quite easily possible with a canvas, a background image, and then gui elements on top, with scrollbars so you're not limited to the size of the form.

place and absolutely positioned items on top of packed ones

KPV: I use the place/pack technique in Slippy Map Demo. I don't know exactly how it works but suspect that place totally ignores what pack is doing and just lays out its widgets in the window.

Ro: I see how you used the same trick to put your map controls and make them absolutely positioned on your map. Nice.