[Tkinter-discuss] A function to allow easier use of gridding in Tk
widgets.
Kenneth McDonald
kmmcdonald at wisc.edu
Sat Apr 3 18:45:27 EST 2004
I poked around in Tkinter, but couldn't find a way to make use of Tk's
ability
to specify grid layouts in a formatted way which gives a sort of a
textual
visual representation of the actual layout. Here's a little function
which does
this. Sorry, at the moment everything is inserted with sticky='news',
and
you still have to do column/row weighting the old way. I haven't used
this
much, so be on the lookout for bugs.
Ken
-------------------------------
def grid(specs, sticky='news', widget=None):
'''
Function to allow easy use of Tk's formatted gridding specification.
Example:
...
title=Label(self, text='Title', background='lightgray',
justify='left', relief='groove')
itext = Text(self, width=width)
itext.configure(yscrollcommand=hscroll.set,
xscrollcommand=vscroll.set)
vscroll.configure(command=itext.xview)
hscroll.configure(command=itext.yview)
grid([
[title, '-' ],
[itext, vscroll ],
[hscroll, '^' ]
])
...
results in a title occupying row 0, columns 0-1; a text object
in row=1, column=0; a horizontal scrollbar in row=2, col=0;
and a vertical scrollbar in rows=1-2, col=1. '-' indicates
carry cell in previous column into this column, '^' indicates
carry call in previous row into this row. 'x' can be used
to indicate a grid cell that should be left empty.
See the Tk 'grid' man page for more details.
'''
# If we aren't passed a widget via which to call
# from tk, we need to find one. The only reason
# to pass in a widget is efficiency, and I doubt
# that it would make any perceptible difference
# anyway.
tk = None
if widget==None:
for row in specs:
for cell in row:
if type(cell) != type(""):
tk = cell.tk
break
if tk:
break
else: tk=widget.tk
for row in specs:
tkrow = []
for cell in row:
if cell=='-' or cell=='^' or cell=='x':
tkrow.append(cell)
else:
tkrow.append(cell._w)
if sticky:
tkrow.append('-sticky')
tkrow.append(sticky)
tk.call('grid', *tkrow)
More information about the Tkinter-discuss
mailing list