Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 9th, 2012, 09:17 AM
drunkskater drunkskater is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 drunkskater User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 15 sec
Reputation Power: 0
[Solved] Problem binding events

EDIT : Problem has nothing to do with nested functions.

Hello,

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 :

Code:
#!/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) :
        print("ajouter_sommet")
        (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) :
        print("ajouter_arete")
        (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) :
            print("actualiser")
            self.delete(self.ligne)
            self.ligne = self.create_line(x_ini, y_ini, 
                                                  event.x, event.y, width = 3)

        def annuler(event) :
            print("annuler")
            self.delete(self.ligne)
            self.unbind("<Motion>")
            self.bind("<Button-1>", self.ajouter_sommet)
            self.tag_bind("sommet", "<Button-1>", self.ajouter_arete)
                   
        def ajouter(event) :
            print("ajouter")
            self.unbind("<Motion>")
            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()
    Dessin(test).pack()
    test.mainloop()


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 :

URL

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

Reply With Quote
  #2  
Old August 9th, 2012, 02:42 PM
drunkskater drunkskater is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 drunkskater User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 15 sec
Reputation Power: 0
Problem solved. I got the answer on a French forum, but I can't post the url because I'm new to the board.

Reply With Quote
  #3  
Old August 9th, 2012, 07:01 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 9 m 51 sec
Reputation Power: 383
Glad it's solved

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Problem binding events within nested functions

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap