Tkinter grid layout

Richard Lewis richardlewis at fastmail.co.uk
Wed Jul 6 10:21:04 EDT 2005


On Wed, 06 Jul 2005 11:44:55 +0100, "Richard Lewis"
<richardlewis at fastmail.co.uk> said:
> 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):
> 
OK, I've changed it so that the Tree and scroll bars are in a single
Frame and added another Frame on the right hand side to put the rest of
my stuff in. (Though I believe this is against the principles of grid
layout management?)

class MainWindow(Frame):
  def __init__(self, master):
    Frame.__init__(self, master)
    self.document = # new DOM document
    self.create_ui()

  def create_ui(self):
    self.master.protocol("WM_DELETE_WINDOW", self.app_quit)

    self.pack(fill="both")
    self.master.geometry("%dx%d%+d%+d" % (800, 600, 100, 100))
    self.master.title("Site Editor")

    self.create_menu()

    self.create_site_list()

    self.rhs_frame = Frame(master=self, background="#FF0000")
    self.rhs_frame.grid(row=0, column=1, sticky=E+W+N+S)
    self.grid_columnconfigure(1, weight=1)
    self.grid_rowconfigure(0, weight=1)

    label = Label(master=self.rhs_frame, text="FOO")
    label.grid(row=0, column=0)

  def create_site_list(self):
    self.list_model = ListModel(self.document)

    self.site_list_frame = Frame(master=self, width=300,
    background="#00FF00")
    self.site_list_frame.grid(row=0, column=0, sticky=E+W+N+S)

    self.site_list = Tree.Tree(master=self.site_list_frame,\
      root_id="root",\
      root_label="Site",\
      get_contents_callback=self.get_list_item,\
      width=300,height=600)

    self.site_list.grid(row=0, column=0, sticky=E+W+N+S)

    self.site_list_frame.grid_columnconfigure(0, weight=1)
    self.site_list_frame.grid_rowconfigure(0, weight=1)

    vsb = Scrollbar(self.site_list_frame, orient=VERTICAL)
    vsb.grid(row=0, column=1, sticky=NS)
    self.site_list.configure(yscrollcommand=vsb.set)
    vsb.configure(command=self.site_list.yview)

    hsb = Scrollbar(self.site_list_frame, orient=HORIZONTAL)
    hsb.grid(row=1, column=0, sticky=EW)
    self.site_list.configure(xscrollcommand=hsb.set)
    hsb.configure(command=self.site_list.xview)

    self.site_list.focus_set()


I noticed that I had a pack() call in my create_ui() function which I
must have pasted in from some example code a while ago. I tried taking
this out (having read that you shouldn't mix pack and grid) but, of
course, then there was no layout (or something) and I just got a blank
window. So I tried using pack(fill="both"). It now fills horizontally
correctly, but it still doesn't anchor with the bottom. I'm not setting
any height for the Tree (if I do then it still doesn't anchor with the
bottom) because I don't want it to have a particular height, just to
fill the window.

Am I heading in the right direction?

Cheers,
Richard



More information about the Python-list mailing list