Formatting logically nested actions -- Pythonic way?
Lie Ryan
lie.1296 at gmail.com
Fri Dec 4 14:57:49 EST 2009
On 12/4/2009 5:24 PM, Alf P. Steinbach wrote:
> Hi.
>
> I discovered with tkinter the registration of widgets with layout
> managers (tkinter "geometry" managers, e.g. calls to pack()) needs to be
> done very hierarchically.
>
> And this leads to hierarchical code, which would be nice to indicate by
> indenting, but oops, indenting in Python is syntactically significant...
>
> So first I thought of one routine per widget creation & layout
> registration, but that was very ugly and verbose. Then thought of simply
> using semicolons but I didn't even try that, because I imagined the
> sheer ugliness. Then I sort of landed on the solution below, but
> although using the language to define a kind of special purpose
> mini-language is common in C++ and Lisp I haven't seen so much of that
> in Python examples, and so I wonder whether this is Pythonic or perhaps
> so extremely un-Pythonic (unconventional) that it's scary -- I mean,
> calls that do nothing whatsoever, but I think of *visual structure* as
> very important here and IMHO (disregarding Pythonicity issues) worth the
> overhead...
maybe you can extend it a bit and make it more useful by auto-packing
the widget when the with-block ended. As for the case when you need to
pass arguments to the packer, perhaps the packing manager's argument
could be turned into an attribute like so:
import tkinter as tk
root = tk.Tk()
with Place(root, tk.Frame()) as display_area:
pic = tk.PhotoImage( file = "lightbulb_off.gif" )
with Pack(display_area, tk.Label) as pic_display:
pic_display.image = pic
# or perhaps just pic_display.side = "left"
pic_display.pack_side = "left"
with Pack(display_area, tk.Frame) as interaction_area:
interaction_area.width = 500
with Pack(interaction_area, tk.Label) as status_line:
status_line.text = "The switch is OFF"
status_line.pack_anchor = "w"
with Pack(interaction_area, tk.Button) as toggle_button:
toggle_button.text = " Toggle it "
toggle_button.pack_anchor = "w"
interaction_area.pack_side = "left"
display_area.place_relx = 0.5
display_area.place_rely = 0.5
display_area.place_anchor = "center"
something like this would make working with GUI much less painful...
More information about the Python-list
mailing list