tkinter:将鼠标滚轮绑定到滚动条 [英] tkinter: binding mousewheel to scrollbar

查看:555
本文介绍了tkinter:将鼠标滚轮绑定到滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个可滚动框架(实际上是画布内的框架).

I have this scroll-able frame (frame inside canvas actually).

import Tkinter as tk
class Scrollbarframe():
    def __init__(self, parent,xsize,ysize,xcod,ycod):
        def ScrollAll(event):
                canvas1.configure(scrollregion=canvas1.bbox("all"),width=xsize,height=ysize,bg='white')
        self.parent=parent
        self.frame1=tk.Frame(parent,bg='white')
        self.frame1.place(x=xcod,y=ycod)
        canvas1=tk.Canvas(self.frame1)
        self.frame2=tk.Frame(canvas1,bg='white',relief='groove',bd=1,width=1230,height=430)
        scrollbar1=tk.Scrollbar(self.frame1,orient="vertical",command=canvas1.yview)
        canvas1.configure(yscrollcommand=scrollbar1.set)
        scrollbar1.pack(side="right",fill="y")
        canvas1.pack(side="left")
        canvas1.create_window((0,0),window=self.frame2,anchor='nw')
        self.frame2.bind("<Configure>",ScrollAll)

我想将鼠标滚轮绑定到滚动条,以便用户可以向下滚动框架,而不必使用滚动条上的箭头按钮.环顾四周后,我向这样的canvas1添加了绑定

I would like to bind mouse wheel to the scrollbar so that user can scroll down the frame without having to use arrow buttons on the scrollbar. After looking around, i added a binding to my canvas1 like this

self.frame1.bind("<MouseWheel>", self.OnMouseWheel)

这是功能:

def OnMouseWheel(self,event):
    self.scrollbar1.yview("scroll",event.delta,"units")
    return "break" 

但是当我使用鼠标滚轮时,滚动条不会移动.谁能帮我这个?我想要的只是当用户使用鼠标滚轮(在框区域内/滚动条上)时,画布应自动向上或向下滚动.

But the scroll bar won't move when i use mousewheel. Can anyone help me with this? All i want is when the user use mousewheel (inside the frame area/on the scrollbar), the canvas should automatically scroll up or down.

推荐答案

也许最简单的解决方案是为鼠标滚轮建立全局绑定.然后,无论鼠标下方是哪个窗口小部件,还是具有键盘焦点的窗口小部件,它都将触发.然后,您可以无条件地滚动画布,或者可以聪明地找出应该滚动的窗口.

Perhaps the simplest solution is to make a global binding for the mousewheel. It will then fire no matter what widget is under the mouse or which widget has the keyboard focus. You can then unconditionally scroll the canvas, or you can be smart and figure out which of your windows should scroll.

例如,在Windows上,您将执行以下操作:

For example, on windows you would do something like this:

self.canvas = Canvas(...)
self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
...
def _on_mousewheel(self, event):
    self.canvas.yview_scroll(-1*(event.delta/120), "units")

请注意,self.canvas.bind_all有点误导-您更正确地应该调用root.bind_all,但是我不知道您如何定义根窗口或如何定义根窗口.无论如何,这两个调用是同义词.

Note that self.canvas.bind_all is a bit misleading -- you more correctly should call root.bind_all but I don't know what or how you define your root window. Regardless, the two calls are synonymous.

平台差异:

  • 在Windows上,您绑定到<MouseWheel>,并且需要将event.delta除以120(或其他一些因素,具体取决于滚动速度)
  • 在OSX上,您绑定到<MouseWheel>,并且需要使用event.delta而不进行修改
  • 在X11系统上,您需要绑定到<Button-4><Button-5>,并且需要将event.delta除以120(或其他一些因素,具体取决于您要滚动的速度)
  • On Windows, you bind to <MouseWheel> and you need to divide event.delta by 120 (or some other factor depending on how fast you want the scroll)
  • on OSX, you bind to <MouseWheel> and you need to use event.delta without modification
  • on X11 systems you need to bind to <Button-4> and <Button-5>, and you need to divide event.delta by 120 (or some other factor depending on how fast you want to scroll)

还有更多涉及虚拟事件的精致解决方案,这些解决方案可以确定哪个窗口具有焦点或位于鼠标下方,或者通过绑定传递画布窗口引用,但是希望这可以帮助您入门.

There are more refined solutions involving virtual events and determining which window has the focus or is under the mouse, or passing the canvas window reference through the binding, but hopefully this will get you started.

这篇关于tkinter:将鼠标滚轮绑定到滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆