tags:

views:

26

answers:

1

I am using Python to parse entries from a log file, and display the entry contents using Tkinter and so far it's been excellent. The output is a grid of label widgets, but sometimes there are more rows than can be displayed on the screen. I'd like to add a scrollbar, which looks like it should be very easy, but I can't figure it out.

The documentation implies that only the List, Textbox, Canvas and Entry widgets support the scrollbar interface. None of these appear to be suitable for displaying a grid of widgets. It's possible to put arbitrary widgets in a Canvas widget, but you appear to have to use absolute co-ordinates, so I wouldn't be able to use the grid layout manager?

I've tried putting the widget grid into a Frame, but that doesn't seem to support the scrollbar interface, so this doesn't work:

mainframe = Frame(root, yscrollcommand=scrollbar.set)

Can anyone suggest a way round this limitation? I'd hate to have to rewrite in PyQt and increase my executable image size by so much, just to add a scrollbar!

+1  A: 

Create a canvas widget and associate the scrollbars with that widget. Then, into that canvas embed the frame that contains your label widgets. Compute the width/height of the frame and feed that into the canvas scrollregion option so that the scrollregion exactly matches the size of the frame.

Drawing the text items directly on the canvas isn't very hard, so you might want to reconsider that approach if the frame-embedded-in-a-canvas solution seems too complex. Since you're creating a grid, the coordinates of each text item is going to be very easy to compute, especially if each row is the same height (which it probably is if you're using a single font).

For drawing directly on the canvas, just figure out the line height of the font you're using (and there are commands for that). Then, each y coordinate is $row*($lineheight+$spacing). The x coordinate will be a fixed number based on the widest item in each column. If you give everything a tag for the column it is in, you can adjust the x coordinate and width of all items in a column with a single command.

Here's an example of the frame-embedded-in-canvas solution:

from Tkinter import *

class App:
    def __init__(self, root):

        self.canvas = Canvas(root, borderwidth=0, background="#ffffff")
        self.frame = Frame(self.canvas, background="#ffffff")
        self.vsb = Scrollbar(root, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)

        self.vsb.pack(side="right", fill="y")
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas.create_window((4,4), window=self.frame, anchor="nw", 
                                  tags="self.frame")

        self.frame.bind("<Configure>", self.OnFrameConfigure)

        self.populate()

    def populate(self):
        '''Put in some fake data'''
        for row in range(100):
            Label(self.frame, text="%s" % row, width=3, borderwidth="1", 
                  relief="solid").grid(row=row, column=0)
            t="this is the second colum for row %s" %row
            Label(self.frame, text=t).grid(row=row, column=1)

    def OnFrameConfigure(self, event):
        '''Reset the scroll region to encompass the inner frame'''
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

if __name__ == "__main__":
    root=Tk()
    app=App(root)
    root.mainloop()
Bryan Oakley
I'm trying this now. To start I'm just loading the data into a frame then putting the frame in a canvas, but the window isn't sizing to the canvas, and the grid options to determine the frame geometry aren't working. I'll post an update if I make any progress.
Simon Hibbs
@Simon Hibbs: I added an example that illustrates how to do it.
Bryan Oakley
That's excellent, thanks. I've got it working nicely. What's the intent of the width and height variables in OnFrameConfigure though? They aren't currently used and the application window isn't being set to the bounds of the canvas, which I'd expect. That's just finessing things though, thanks for your help.
Simon Hibbs
@Simon Hibbs: width and height was mistakenly left in. I'll remove them.
Bryan Oakley