Tkinter: can widgets automatically resize to fit parent?
Eric Brunel
eric.brunel at pragmadev.com
Wed Mar 6 13:04:45 EST 2002
Andy Gimblett wrote:
> I want my Tk widgets (some of them, at least) to resize dynamically
> with the window.
>
> Trivial example:
>
> #!/usr/bin/env python
>
> from Tkinter import *
>
> class Application(Frame):
>
> def __init__(self, master):
> Frame.__init__(self, master)
> self.action = Button(self.master, text="Go")
> self.action.grid(sticky=N+E+W+S)
Two reasons why it's not working:
- you didn't actually display your Application in its master. The
"Frame.__init__" stuff does not suffice: you should also do a pack or grid.
Then, inserting the button in self.master rather than in self should be
useless...
- you configured your button to take the size of its parent cell using the
sticky option, but you didn't tell the cell to take the whole space in its
parent. This should be done via the grid_rowconfigure and
grid_columnconfigure methods to set the weight option on the cell.
So here is a code that does what you want:
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
## Actually insert self in its master
self.pack(fill=BOTH, expand=1)
## Configure cell at (0, 0) to take the whole space
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
## Insert button in self rather than self.master
self.action = Button(self, text="Go")
self.action.grid(sticky=N+E+W+S)
etc...
HTH
- eric -
More information about the Python-list
mailing list