A
Alf P. Steinbach
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...
<code>
import tkinter as tk
import contextlib
@contextlib.contextmanager
def this( object ):
yield object
window = tk.Tk()
window.title( "Picture presentation test" )
with this( tk.Frame() ) as display_area:
pic = tk.PhotoImage( file = "lightbulb_off.gif" )
with this( tk.Label( display_area, image = pic ) ) as pic_display:
pic_display.pack( side = "left" )
with this( tk.Frame( display_area, width = 500 ) ) as interaction_area:
with this( tk.Label( interaction_area ) ) as status_line:
status_line.config( text = "The switch is OFF" )
status_line.pack( anchor = "w" )
with this( tk.Button( interaction_area ) ) as toggle_button:
toggle_button.config( text = " Toggle it " )
toggle_button.pack( anchor = "w" )
interaction_area.pack( side = "left" )
display_area.place( relx = 0.5, rely = 0.5, anchor = "center" ) # Centered
window.mainloop()
</code>
Cheers,
- Alf
PS: I see a non-functional width specification in there. Just forgot to remove
that. And better with code as-is straight from editor than fixing up manually!
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...
<code>
import tkinter as tk
import contextlib
@contextlib.contextmanager
def this( object ):
yield object
window = tk.Tk()
window.title( "Picture presentation test" )
with this( tk.Frame() ) as display_area:
pic = tk.PhotoImage( file = "lightbulb_off.gif" )
with this( tk.Label( display_area, image = pic ) ) as pic_display:
pic_display.pack( side = "left" )
with this( tk.Frame( display_area, width = 500 ) ) as interaction_area:
with this( tk.Label( interaction_area ) ) as status_line:
status_line.config( text = "The switch is OFF" )
status_line.pack( anchor = "w" )
with this( tk.Button( interaction_area ) ) as toggle_button:
toggle_button.config( text = " Toggle it " )
toggle_button.pack( anchor = "w" )
interaction_area.pack( side = "left" )
display_area.place( relx = 0.5, rely = 0.5, anchor = "center" ) # Centered
window.mainloop()
</code>
Cheers,
- Alf
PS: I see a non-functional width specification in there. Just forgot to remove
that. And better with code as-is straight from editor than fixing up manually!