Python的Tkinter的结合鼠标滚轮滚动条来 [英] Python tkinter binding mousewheel to scrollbar

查看:8559
本文介绍了Python的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 像这样

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中,您绑定到&LT;鼠标滚轮&GT; ,你需要120分 event.delta (或一些其他的因素取决于你如何想快速滚动)

  • 在OSX,绑定到&LT;鼠标滚轮&GT; 键,你需要使用 event.delta 无需修改

  • 在X11系统
  • 您需要绑定到&LT;扣4 GT; &LT;扣5 GT; ,你需要120分 event.delta (或取决于你想如何快速滚动其他因素)

  • 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.

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

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