[Tutor] Tkinter Grid Layout Problems

Alan Gauld alan.gauld at btinternet.com
Mon Nov 26 10:05:07 CET 2007


"Johnston Jiaa" <oclbdk at gmail.com> wrote

> I'm trying to get the left part of a window to look like this:
> http://img.photobucket.com/albums/v228/gaypig123ABC/PB240055.jpg
>
> I tried using sticky's, but nothing seems to work correctly.
>
I did it this way:
#####################
from Tkinter import *

listItems = """first second third
               fourth fifth sixth
               seventh eighth ninth
               tenth eleventh twelth""".split()

class App:
   def __init__(self, parent):
      self.lt_lbl = Label(parent, text="Left label")
      self.lt_lbl.grid(row=0, column=0)
      self.lt_add_btn = Button(parent, text="Left add button")
      self.lt_add_btn.grid(row=0, column=1)
      self.lt_rem_btn = Button(parent, text="Left remove button")
      self.lt_rem_btn.grid(row=0, column=2)

      scrollList = Frame(parent)
      self.lt_lstbx = Listbox(scrollList)
      self.lt_lstbx.pack(side=LEFT)
      self.lt_scrlbr = Scrollbar(scrollList)
      self.lt_scrlbr.pack(side=RIGHT, fill="y" )
      scrollList.grid(row=1, column=0)

      for item in listItems:
         self.lt_lstbx.insert(END,item)

tk = Tk()
app = App(tk)
tk.mainloop()

Note that I created a frame to hold both list box and scrollbar and 
used
the packer to put them together.

> How do I get everything aligned properly?

However I would personally use the packer rather than the grid for the
whole GUI.
I'd create a frame for your buttons and label and pack them with
the label on the left and buttons on the right, then another frame
for the scrolled list as above then simply pack the two frames into
the root window.

YMMV,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list