1

I've searched around and cannot seem to find an answer for my problem. I am trying to create a working scrollbar for the following code and cannot seem to get it to work. The problem appears to be with the OnFrameConfigure method. I have seen elsewhere that the method should be def OnFrameConfigure(event):however when I place the (event) part into my method it does not work unless I write the function outside of a class

 class Main(tk.Tk):

    def __init__(self, *args, **kwargs):
        '''This initialisation runs the whole program'''

        #tk.Tk.__init__(self, *args, **kwargs)
        main =  tk.Tk()
        canvas = tk.Canvas(main)
        scroll = tk.Scrollbar(main, orient='vertical', command=canvas.yview)
        canvas.configure(yscrollcommand=scroll.set)
        frame = tk.Frame(canvas)
        scroll.pack(side='right', fill='y')
        canvas.pack(side='left', fill='both', expand='yes')
        canvas.create_window((0,0), window=frame)
        frame.bind('<Configure>', self.OnFrameConfigure(parent=canvas))

        for i in range(100):
            tk.Label(frame, text='I am a Label').pack()

        main.mainloop()


    def OnFrameConfigure(self, parent):
        '''Used to allowed scrolled region in a canvas'''
        parent.configure(scrollregion=parent.bbox('all'))  
firebird92
  • 119
  • 8

1 Answers1

2

Your problem starts here:

frame.bind('<Configure>', self.OnFrameConfigure(parent=canvas))

You are immediately calling the OnFrameConfigure function. That is not how you use bind. You must give a reference to a callable function. Since you're using a class, you don't need to pass parent in, unless you have this one function work for multiple widgets.

Change the binding to this:

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

Change the method definition to this:

def OnFrameConfigure(self, event):

Finally, your __init__ needs to save a reference to the canvas, which you can then use in that function:

def __init__(self, *args, **kwargs):
    ...
    self.canvas = tk.Canvas(...)
    ...

...
def OnFrameConfigure(self, event):
    ...
    self.canvas.configure(scrollregion=self.canvas.bbox('all'))   
    ...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Lets say that I needed `def OnFrameConfigure` for multiple widgets and therefore needed the `(parent)` argument how would I add this? I tried lambda however and this only seems to work for things with a `command` – firebird92 Jun 15 '15 at 15:14
  • 1
    @firebird92: you would use lambda; there's nothing special about lambda that makes it only work with the command attribute. See http://stackoverflow.com/q/28995395/7432, or search this site for `[tkinter] bind lambda` for many examples. – Bryan Oakley Jun 15 '15 at 16:27