1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Rep Power

    [Solved] Problem binding events

    EDIT : Problem has nothing to do with nested functions.


    I'm having trouble with tkinter binding events inside nested functions.

    I'm using Python 3.1

    Here is an example. Sorry for the french names, but they aren't really important. If you really want to know : ajouter ~ add ; sommet ~ node ; arÍte ~ edge ; annuler ~ cancel ; actualiser ~ refresh

    Anyway, here is the example :

    #!/usr/bin/env python3
    from tkinter import *
    class Dessin(Canvas) :
        "Permet de tracer des points rouges et de les relier par des traits noirs"
        def __init__(self, boss) :
            Canvas.__init__(self, boss, width = 650, height = 250, bg = "white")
            self.bind("<Button-1>", self.ajouter_sommet)
        def ajouter_sommet(self, event) :
            (x, y) = (event.x, event.y)
            ref = self.create_oval(x - 10, y - 10, x + 10, y + 10, fill = "red")
            self.addtag_withtag("sommet", ref)
            self.tag_bind(ref, "<Button-1>", self.ajouter_arete)
        def ajouter_arete(self, event) :
            (x_ini, y_ini) = (event.x, event.y)
            self.ligne = self.create_line(x_ini, y_ini, x_ini, y_ini, width = 3)
            def actualiser(event) :
                self.ligne = self.create_line(x_ini, y_ini, 
                                                      event.x, event.y, width = 3)
            def annuler(event) :
                self.bind("<Button-1>", self.ajouter_sommet)
                self.tag_bind("sommet", "<Button-1>", self.ajouter_arete)
            def ajouter(event) :
                self.bind("<Button-1>", self.ajouter_sommet)
                self.tag_bind("sommet", "<Button-1>", self.ajouter_arete)
            self.bind("<Motion>", actualiser)
            self.bind("<Button-1>", annuler)
            self.tag_bind("sommet", "<Button-1>", ajouter)
    # --- Test de la classe ---
    if __name__ == '__main__' :
        test = Tk()
    I'd like this program to do this : upon left-click, draw a point (= a node). If the next click is on an existing node, draw a black line from the node to the pointer until another node is right-clicked on. But if it isn't, then cancel the operation and stop drawing the line.

    Unless there is a more elegant solution, I really want to use nested functions : first, thanks to the free variables inside the outer function, I don't have to use arguments for the inner functions (in my real code they have more arguments that than...). And I don't want to have a billion meaning-less micro-function inside my program. I think it's much more logical to group functions like this.

    Here is my problem :

    when ajouter_arete is called by bind following <Button-1>, so is annuler !

    For instance, here is a run of the script above :

    The two nodes are created all right, but when I click on one of them, nothing seems to happen. Except that this is due to the fact that annuler has been called...

    I don't know how events work, but could it be because when a function is called by bind, the "event queue" is popped only when the function called has finished executing ? If so, is there a way to "force" the "popping" of the head of the queue ?

    So, how could I solve this problem ?

    By the way, I'm completely new to Python and to GUI, but I'm not new to programming.

    Thanks a lot !
    Last edited by drunkskater; August 9th, 2012 at 02:40 PM. Reason: Solved
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Rep Power
    Problem solved. I got the answer on a French forum, but I can't post the url because I'm new to the board.
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power

    Glad it's solved

    I agree, nesting was not the trouble.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo