python tkinter frame滚动条_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("",ScrollAll)

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("", self.OnMouseWheel)

This is the function:

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.

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

self.canvas = Canvas(...)

self.canvas.bind_all("", self._on_mousewheel)

...

def _on_mousewheel(self, event):

self.canvas.yview_scroll(-1*(event.delta/120), "units")

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.

Platform differences:

On Windows, you bind to 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 and you need to use event.delta without modification

on X11 systems you need to bind to and , 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值