Tkinter grid layout
William Gill
noreply at gcgroup.net
Wed Jul 6 12:32:42 EDT 2005
Excuse me for intruding, but I followed examples and ended up with a
similar architecture:
from Tkinter import *
class MyMain(Frame):
def __init__(self, master):
self.root = master
self.master=master
root = Tk()
app = MyMain(root)
app.master.title("Object Editor")
root.mainloop()
Erick, are you saying it should be modified to something like :
from Tkinter import *
class MyMain(Tk):
...
...
app = MyMain()
app.title("My App")
app.mainloop()
Thanks,
Bill
Eric Brunel wrote:
> On Wed, 06 Jul 2005 11:44:55 +0100, Richard Lewis
> <richardlewis at fastmail.co.uk> wrote:
>
>> Hi there,
>>
>> I've got a tree control in Tkinter (using the ESRF Tree module) but I
>> can't get it to layout how I want it.
>>
>> I'd like to have it so that it streches north/south (anchored to the top
>> and bottom), is of a fixed width and is anchored to the left hand side.
>> Here's my code (its derived from one of the examples from the ESRF web
>> site):
>>
>> class MainWindow(Frame):
>
> [snip code]
>
> First of all, it is not a good idea to make your so-called window
> inherit from Frame: a Frame is not a window in tk, but a generic
> container. Creating a Frame without a container window will
> automatically initialize the tk toolkit, creating a default window which
> will become the default parent for widgets where you don't specify one.
> According to the well-known "explicit is better than implicit"
> principle, if a MainWindow instance are actually the main window for
> your application, you'd really better inherit from the Tkinter class for
> the main window, which is Tk. Doing it this way has a lot of advantages
> over what you do; for example, if you later have to create a menu bar
> for your application, you will be able to do it in the MainWindow class.
> If you inherit from Frame, you won't get any access to the actual
> window, so you won't be able to create a menu in it.
>
> This may seems to be unrelated to your problem, but it's not: by
> creating a Frame, you introduce one more unneeded container. So, in some
> code you don't show here, you have to insert the MainWindow instance
> into its parent window via a call to its pack or grid method. Since the
> code you show seems to be correct, I guess the problem is in this call
> to pack or grid, which probably does not tell the Frame how to behave
> when its parent window is resized, causing it to get the default
> behaviour, which is "do nothing at all".
>
> To be sure this actually is the problem, try to replace the line:
> Frame.__init__(self, master)
> in MainWindow.__init__ by:
> Frame.__init__(self, master, bg='blue', bd=2)
> This way, a blue border will appear around the frame, allowing you to
> see how it grows.
> Then run your application, and resize the window. You should see that
> the frame does not grow when the window grows, explaining why your tree
> deos not grow (in fact, it would grow if its container did; but its
> container doesn't...)
>
> So you should either make your MainWindow class inherit from Tk, which
> eliminates the unneeded container and the problems it may cause, or make
> sure the pack or grid on your MainWindow instance actually tells the
> container to grow with its container. With pack, it's quite easy: just
> do myWindow.pack(fill=BOTH, expand=1). With grid, it's a bit more
> complicated, since you will have to configure the grid on the container.
>
> But basically, my advice would be:
> - Never make your windows inherit from Frame; make them inherit from Tk
> for the main window or from Toplevel for all other ones
> - When you have resize problems, always check the whole widget hierarchy
> from the actual window down to the widget showing the problem. The cause
> is very often not on the widget itself, but on one of its containers.
>
> HTH
More information about the Python-list
mailing list