I have added some scrollbar functionality to my Tkinter GUI like so
self.canvas = tk.Canvas(self.baseframe)
self.scrollbar = tk.Scrollbar(self.baseframe, orient="vertical", command=self.canvas.yview)
self.scrollable = tk.Frame(self.canvas)
self.scrollable.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
self.canvas.create_window((0, 0), window=self.scrollable, anchor="n")
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.bind_all("<MouseWheel>", self._mousescroll)
I have defined _mousecroll()
as
def _mousescroll(self, event):
"""mouse wheel scroll callback"""
# Divide the event.delta by some value to effect the scrolling speed
self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")
which is working nicely.
I then add some tkLabelFrame
widgets to the scroll area (which themselves contain widgets displaying stuff - not relevant for this question). The whole scrolling functionality works really well.
In other part of my GUI, I would like the user to be able to "jump" to a specific point in the scroll area, on a button click. In effect automatically scroll the scroll area to this point. Is this possible? I have full control over the GUI code so I can add labels to the list of tkLabelFrames, store them in some kind of list/dictionary so they can easily be looked up...something like that?
Note: The number of tkLabelFrames
that are added to the scroll area is dynamic so some kind of absolute "jump to this many pixels" would probably not work. I was hoping there might be some kind of "scroll to a widget with this label/attribute etc.." functionality. Any help appreciated. Thanks.
For my particular scenario, the widgets within the scrollable area where all the same height, so I was able to use a simple fraction to scroll to the correct place.
Adding a button, with a callback to a method like this worked for me:
def scrollto(self, idx):
# Where self.frames is the list of tkLabelFrame objects.
fraction = float(idx/len(self.frames))
# self.canvas is my scrollable canvas
self.canvas.yview_moveto(fraction)