Tkinter: scrollbar in grid-managed frame

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anx@io.us

    #1

    Tkinter: scrollbar in grid-managed frame

    I've got a grid-managed frame, containing a column of Labels, and a
    corresponding column of Entry widgets. I'd like to be able to display
    dozens, or even hundreds of rows, and use a vertical scrollbar to
    scroll through them in the frame. So far i can get a scrollbar to
    display, but it won't scroll anything. So that's definately wrong. And
    the columns just chop off at the bottom of the frame, but resizing the
    window won't make the frame show more rows. Here's a rough sketch of
    what i've done:

    mw = Tk()
    mainFrame = Frame( mw, borderwidth=5, relief=GROOVE, )
    mainFrame.pack( side=TOP, anchor=N, expand=True, fill=BOTH, )

    vertSB = Scrollbar( mainFrame, orient=VERTICAL , )
    vertSB.grid( padx=1, column=0, rowspan=15, columnspan=1, sticky=NS, )
    mainFrame.colum nconfigure( 0, weight=1 )

    Next i iterate through a simple dict, constructing a Label (in column
    1) for each key, and an Entry (in column 2) for each value.

    How do i make the Scrollbar fill all of column 0, no matter how many
    rows are displayed, and stretch/shrink with mainFrame's size? Do i
    have to catch resizing events, calculate how many rows are now
    showing, and adjust rowspan?

    How do i make my grid show more rows when i expand my window?

    Am i going about this entirely the wrong way? Is grid the wrong
    geometry manager to use for this frame?

    Thanks,
    E

    --
  • jepler@unpythonic.net

    #2
    Re: Tkinter: scrollbar in grid-managed frame

    Tkinter "frame"s don't scroll. Instead, you need to use something like
    bwidget's "ScrollableFram e" widget. You may want to combine this with
    bwidget's "ScrolledWindow ".

    Below is an example which uses my "pybwidget" package, available at

    # ----------------------------------------------------------------------
    import Tkinter, bwidget

    t = Tkinter.Tk()
    s = bwidget.Scrolle dWindow(t, auto="vertical" , scrollbar="vert ical")
    f = bwidget.Scrolla bleFrame(s, constrainedwidt h=True)
    g = f.getframe()

    for i in range(20):
    Tkinter.Label(g , text="Field %d: " % i).grid(row=i, column=0, sticky="w")
    Tkinter.Entry(g , width=25).grid( row=i, column=1, sticky="ew")
    g.grid_columnco nfigure(1, weight=1)

    s.setwidget(f)
    s.pack(fill="bo th", expand=1)
    t.mainloop()
    # ----------------------------------------------------------------------

    Jeff


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDe6kEJd0 1MZaTXX0RAqf+AJ wJeqXh+1j4tiJak dM1SA59kGccDgCe IUE/
    elqfqeLfW2Q7nqs KN8qSpsQ=
    =oicG
    -----END PGP SIGNATURE-----

    Comment

    • jepler@unpythonic.net

      #3
      Re: Tkinter: scrollbar in grid-managed frame

      Tkinter "frame"s don't scroll. Instead, you need to use something like
      bwidget's "ScrollableFram e" widget. You may want to combine this with
      bwidget's "ScrolledWindow ".

      Below is an example which uses my "pybwidget" package, available at

      # ----------------------------------------------------------------------
      import Tkinter, bwidget

      t = Tkinter.Tk()
      s = bwidget.Scrolle dWindow(t, auto="vertical" , scrollbar="vert ical")
      f = bwidget.Scrolla bleFrame(s, constrainedwidt h=True)
      g = f.getframe()

      for i in range(20):
      Tkinter.Label(g , text="Field %d: " % i).grid(row=i, column=0, sticky="w")
      Tkinter.Entry(g , width=25).grid( row=i, column=1, sticky="ew")
      g.grid_columnco nfigure(1, weight=1)

      s.setwidget(f)
      s.pack(fill="bo th", expand=1)
      t.mainloop()
      # ----------------------------------------------------------------------

      Jeff


      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.1 (GNU/Linux)

      iD8DBQFDe6kEJd0 1MZaTXX0RAqf+AJ wJeqXh+1j4tiJak dM1SA59kGccDgCe IUE/
      elqfqeLfW2Q7nqs KN8qSpsQ=
      =oicG
      -----END PGP SIGNATURE-----

      Comment

      Working...