[Tutor] Tkinter: Grid : weight option
Evgeny Roubinchtein
eroubinc@u.washington.edu
Thu, 9 Sep 1999 12:09:19 -0700 (PDT)
After my initial frustration with doing a "fileevent" in Python a'la TCL,
I am translating some simpler scripts from TCL/Tk to Python, so I can be
comfortable with widgets, geometry managers and such.
I hit a snag on this script, in particular with the "weight" setting:
#!/usr/local/bin/wish8.0
text .text -yscrollcommand ".yscroll set" \
-xscrollcommand ".xscroll set" \
-width 40 -height 10
scrollbar .yscroll -command ".text yview" -orient vertical
scrollbar .xscroll -command ".text xview" -orient horizontal
grid .text .yscroll -sticky news
grid .xscroll -sticky ew
grid rowconfigure . 0 -weight 1
grid columnconfigure . 0 -weight 1
I looked at Fredrik Lundh's Tkinter tutorial, where he talks about the
Grid Geometry Manager "Manager Options", and decided that one way to
translate the above into Python would be thusly:
#! /usr/local/bin/python
from Tkinter import *
class App(Frame):
def __init__(self, master = None): # as in Matt Conways examples
Frame.__init__(self, master)
Pack.configure(self)
self.create_widgets()
def create_widgets(self):
self.text = Text(self, width = 40, height = 10)
self.yscroll = Scrollbar(self, command = self.text.yview, orient = VERTICAL)
self.xscroll = Scrollbar(self, command = self.text.xview, orient = HORIZONTAL)
self.text.configure(yscrollcommand = self.yscroll.set, xscrollcommand = self.xscroll.set)
self.text.grid(row = 0, column = 0, sticky = 'news')
self.yscroll.grid(row = 0, column = 1, sticky = 'news')
self.xscroll.grid(row = 1, column = 0, sticky = 'ew')
# now set the weights
self.rowconfigure(0, weight = 1)
self.columnconfigure(0, weight = 1)
def test():
app = App()
app.mainloop()
if __name__ == '__main__':
test()
Unfortunately, when I run it the text area and the scrollbars shrink
when I make the window smaller, but they won't grow when I make the
window larger (resizing the window in my window manager). I am a bit
puzzled as to why this happens. I did a "grep -i -l weight *.py" in my
the lib-tk directory of my Python installation, and the only mention of
weight appears to be in the tkFont module, which puzzles me even more.
This is Python 1.5.2 running under FreeBSD-3.2-STABLE, with XFree86 and
AfterStep-1.6.10 as a WM, btw.
--
Evgeny Roubinchtein, eroubinc@u.washington.edu
...................
Performance proven: It works through beta test.