python画布位置_Python Tkinter-使用鼠标位置滚动画布

I think this is a very common question, but I couldn't find the answer.

I'm trying to make a window that scrolls depending on the mouse position: if the mouse is close to top of the screen, it scrolls to the top, if it is close to the right border, it scrolls to the right and so on. Here is the code:

from tkinter import *

from tkinter import ttk

root = Tk()

h = ttk.Scrollbar(root, orient = HORIZONTAL)

v = ttk.Scrollbar(root, orient = VERTICAL)

canvas = Canvas(root, scrollregion = (0, 0, 2000, 2000), width = 600, height = 600, yscrollcommand = v.set, xscrollcommand = h.set)

h['command'] = canvas.xview

v['command'] = canvas.yview

ttk.Sizegrip(root).grid(column=1, row=1, sticky=(S,E))

canvas.grid(column = 0, row = 0, sticky = (N,W,E,S))

h.grid(column = 0, row = 1, sticky = (W,E))

v.grid(column = 1, row = 0, sticky = (N,S))

root.grid_columnconfigure(0, weight = 1)

root.grid_rowconfigure(0, weight = 1)

canvas.create_rectangle((0, 0, 50, 50), fill = 'black')

canvas.create_rectangle((500, 500, 550, 550), fill = 'black')

canvas.create_rectangle((1500, 1500, 1550, 1550), fill = 'black')

canvas.create_rectangle((1000, 1000, 1050, 1050), fill = 'black')

def xy_motion(event):

x, y = event.x, event.y

if x < 30:

delta = -1

canvas.xview('scroll', delta, 'units')

if x > (600 - 30):

delta = 1

canvas.xview('scroll', delta, 'units')

if y < 30:

delta = -1

canvas.yview('scroll', delta, 'units')

if y > (600 - 30):

delta = 1

canvas.yview('scroll', delta, 'units')

canvas.bind('', xy_motion)

root.mainloop()

The problem is that the scrolling movement is in the motion function that only works if there is mouse movement (if you stop moving the mouse, the scrolling stops too). I would like to make it a way that even if the mouse is not moving (but still in the "scrolling area") the window would keep scrolling until it reaches the end.

I thought the obvious way would be changing the if statement (from line 30 for example) to a while statement, like this:

while x < 30:

But then when the mouse reaches this position the program freezes (waiting for the while loop to finish I think).

Any suggestion?

Thanks in advance.

UPDATE

Here is the working code with an (or one of the possible) answer. I don't know if it is right to update the question itself with the answer, but I think it can be useful to others.

x, y = 0, 0

def scroll():

global x, y

if x < 30:

delta = - 1

canvas.xview('scroll', delta, 'units')

elif x > (ws - 30):

delta = 1

canvas.xview('scroll', delta, 'units')

elif y < 30:

delta = -1

canvas.yview('scroll', delta, 'units')

elif y > (ws - 30):

delta = 1

canvas.yview('scroll', delta, 'units')

canvas.after(100, scroll)

def xy_motion(event):

global x, y

x, y = event.x, event.y

scroll()

canvas.bind('', xy_motion)

Please let me know if it is correct. Thanks to everyone for the discussion and suggestions. These links were useful too.

解决方案

First, set up a method that scrolls the window by a tiny amount, then calls itself again after some fixed period of time (eg 100ms) if the mouse is in the region. You can use the method "after" todo this. With this, the canvas will continuously scroll as long as the mouse is in the scroll region.

Next, create a binding that calls this function when the cursor enters the scroll zone for the first time.

And that's all you need. Just make sure you only have one of tbese scroll jobs running at any one time.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值