user2398401
user2398401

Reputation: 3

New to Python, need an event listener equivalent?

At a specific point in my script, I need to set a variable to the co-ordinates clicked on a TKinter canvas. I don't think canvas.bind will work as that just runs a specified function when ever clicked on. What I need is some sort of equivalent to x = raw_input(). Help would be greatly appreciated.

Upvotes: 0

Views: 793

Answers (1)

Jared
Jared

Reputation: 26397

Here is a simple snippet for saving the coordinates from a Canvas widget. Using bind is what you want.

import Tkinter as tk

class Application(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.canvas = tk.Canvas(self.master, width=400, height=400)
        self.canvas.bind('<Button-1>', self.coordinates)
        self.canvas.pack()

    def coordinates(self, event):
        self.x = (event.x, event.y) # set x (or another attr) to coordinate tuple

if __name__ == "__main__":
    root = tk.Tk()
    app = Application(root)
    app.mainloop()

It sounds like this is all you need but if not, you will probably have to provide some of your current code.

Upvotes: 2

Related Questions