我已经在Tkinter GUI中添加了一些滚动条功能,如下所示
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)
我定义_mousecroll()
为
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")
效果很好。
然后,我tkLabelFrame
向滚动区域添加一些小部件(它们本身包含显示内容的小部件-与该问题无关)。整个滚动功能效果很好。
在我的GUI的其他部分,我希望用户能够通过单击按钮来“跳转”到滚动区域中的特定点。实际上,将滚动区域自动滚动到此点。这可能吗?我对GUI代码具有完全控制权,因此我可以将标签添加到tkLabelFrames列表中,将它们存储在某种列表/字典中,以便可以轻松地查找它们……类似的东西?
注意:tkLabelFrames
添加到滚动区域中的数字是动态的,因此某种绝对的“跳转到这么多像素”可能不起作用。我希望可能会有某种“滚动到带有此标签/属性等的小部件”功能。任何帮助表示赞赏。谢谢。
对于我的特定情况,可滚动区域内的小部件高度均相同,因此我能够使用简单的分数来滚动至正确的位置。
添加一个带有回调的按钮对这样的方法对我有用:
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句