HaggarTheHorrible
HaggarTheHorrible

Reputation: 7413

tkinter: How to automatically scroll using middle mouse button?

right now I have an implementation where I'm using my middle mouse button for two operations:

  1. Zooming (in and out) by rolling the wheel and
  2. Scrolling the wheel button to scroll(pan) left, right, top and bottom. But in this feature, I have press and hold the scroll button continuously, only then can I scroll.

What I wish to do is:

  1. Press and release the middle mouse button to enter scrolling mode.
  2. Then, simply, move the mouse left and right and, top and bottom, to scroll in the respective directions.
  3. Once done, press and release the middle mouse button to come out of this mode.

It is just like how it is implemented in MS Word or Chrome browser.

Help!

Upvotes: 0

Views: 1352

Answers (1)

Steven Summers
Steven Summers

Reputation: 5394

Here's a quick simple thing of what I believe you're after.

import tkinter as tk

root = tk.Tk()

pressed = False

def onClick(event):
    global pressed
    pressed = not pressed # toggle pressed when clicked
    print('Pressed')

def onMove(event):
    if pressed:
        print(event.x, event.y)

root.bind('<Button-2>', onClick)
root.bind('<Motion>', onMove)

root.mainloop()

Upvotes: 2

Related Questions