S. Gamgee
S. Gamgee

Reputation: 521

Python (2.*) Tkinter - Advanced Event Handling Formatting

I'm writing a test program to detect mouse motion within a Tkinter Window using Python 2.*. I can create the necessary widgets and bind the appropriate event handler function to the root widget as needed:

import Tkinter as tk

class App:

   def __init__(self, master=None):
      self.root = master
      self.frame = tk.Frame(master)
      self.frame.pack()
      self.create_widgets()
      self.setup_handlers()

   def create_widgets(self):
      ...

   def setup_handlers(self):
      self.root.bind('<Motion>', self.update)   # This is the line I wish to look at

   def update(self, event):
      ...

root = tk.Tk()
app = App(master=root)
root.mainloop()

What I want to do now is be able to activate the event handler with a combined input. I want to be able, for instance, to only activate the event handler when I move the mouse with the 'r' key held down. What event string would I need for this? Where can I find a full rundown on the event-string formatting for binding event handlers?

Upvotes: 1

Views: 151

Answers (1)

LycuiD
LycuiD

Reputation: 2575

for the combined event handling, you can do something like:

class App:
  holding = False
  def __init__(self, master):
    self.root = master
    self.root.bind("<KeyPress>", self.holdkey)
    self.root.bind("<KeyRelease>", self.releasekey)

  def holdkey(self, e):
    if e.char == "r" and not self.holding:
      self.root.bind("<Motion>", self.combined_update)
      self.holding = True

  def releasekey(self, e):
    if e.char == "r" and self.holding:
      self.root.unbind("<Motion>")
      self.holding = False

  def combined_update(self, e):
    # Event handling for the combined event.
    print ("yo")

This will "probably" work.

Upvotes: 1

Related Questions