[Tutor] Dynamic TKinter widgets?

Alan Gauld alan.gauld at btinternet.com
Sun Nov 25 10:24:15 CET 2012


On 25/11/12 03:16, Nathan wrote:

> What I'm trying to do now is add support for the card images. the
> problem is, the program supports multiple types of spreads (two, so far,
> are selectable), and they use different numbers of cards. It looks like
> I need a variable number of widgets to display something like this. I'm
> not entirely sure how I would handle something like that.

Tkinter is dynamic by definition.
So you just need a loop to add your images in some kind of collection 
widget. A Canvas or a Frame would seem obvious choices... Just choose 
your preferred layout manager - simple pack() would work or you could 
use grid() for more sophisticated shapes.

> Right now, though, I do have two ideas that I would like to run by you
> guys.
>
> The first is to use one text widget-- as opposed to the one label widget
> I'm currently using-- and embed the images where the card names would
> be. I would have to try out some demos first, though, to see if I can
> otherwise use text widgets just as label widgets.

You can certainly embed images in Text widgets, I'm not sure about 
Labels, but I think you can. You can also use Buttons.

> The other idea, which seems much more cumbersome, is to create a dummy
> frame widget, then a frame widget designed specifically for each type of
> spread. The user selects the spread they want to use (Celtic Cross), and
> the associated frame gets placed on the GUI.

That doesn't sound cumbersome to me, it sounds like standard Tkinter 
practice. Frames are the base object of nearly everything in Tkinter.
A Celtic cross pattern could just be an algorithm for positioning things 
on a grid... So it may simply be a case of having different generator 
functions for the card locations (a list of tuples?) and selecting a 
function rather than a Frame.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list