oval

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Bush

    #1

    oval

    I tested the following code and wanted to get the message of "oval2
    got hit" if I click the red one. But I always got "oval1 got hit".
    from Tkinter import *
    root=Tk()
    canvas=Canvas(r oot,width=100,h eight=100)
    canvas.pack()
    a=canvas.create _oval(10,10,20, 20,tags='oval1' ,fill='blue')
    b=canvas.create _oval(50,50,80, 80,tags='oval2' ,fill='red')
    def myEvent(event):
    if a:
    print "oval1 got hit!"
    else:
    print "oval2 got hit"
    canvas.tag_bind ('oval1','<Butt on>',func=myEve nt)
    canvas.tag_bind ('oval2','<Butt on>',func=myEve nt)
    root.mainloop()
  • Diez B. Roggisch

    #2
    Re: oval

    Ben Bush wrote:[color=blue]
    > I tested the following code and wanted to get the message of "oval2
    > got hit" if I click the red one. But I always got "oval1 got hit".
    > from Tkinter import *
    > root=Tk()
    > canvas=Canvas(r oot,width=100,h eight=100)
    > canvas.pack()
    > a=canvas.create _oval(10,10,20, 20,tags='oval1' ,fill='blue')
    > b=canvas.create _oval(50,50,80, 80,tags='oval2' ,fill='red')
    > def myEvent(event):
    > if a:[/color]

    Here is your problem. a is a name, bound to some value. So - it is true,
    as python semantics are that way. It would not be true if it was e.g.

    False, [], {}, None, ""


    What you want instead is something like

    if event.source == a:
    ...

    Please note that I don't know what event actually looks like in Tkinter,
    so check the docs what actually gets passed to you.


    Regards,

    Diez

    Comment

    • Ben Bush

      #3
      Re: oval

      On 12/4/05, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=blue]
      > Ben Bush wrote:[color=green]
      > > I tested the following code and wanted to get the message of "oval2
      > > got hit" if I click the red one. But I always got "oval1 got hit".
      > > from Tkinter import *
      > > root=Tk()
      > > canvas=Canvas(r oot,width=100,h eight=100)
      > > canvas.pack()
      > > a=canvas.create _oval(10,10,20, 20,tags='oval1' ,fill='blue')
      > > b=canvas.create _oval(50,50,80, 80,tags='oval2' ,fill='red')
      > > def myEvent(event):
      > > if a:[/color]
      >
      > Here is your problem. a is a name, bound to some value. So - it is true,
      > as python semantics are that way. It would not be true if it was e.g.
      >
      > False, [], {}, None, ""
      >
      >
      > What you want instead is something like
      >
      > if event.source == a:
      > ...
      >
      > Please note that I don't know what event actually looks like in Tkinter,
      > so check the docs what actually gets passed to you.[/color]

      got AttributeError: Event instance has no attribute 'source'

      Comment

      • Peter Otten

        #4
        Re: oval

        Ben Bush wrote:
        [color=blue]
        > On 12/4/05, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=green]
        >> Ben Bush wrote:[color=darkred]
        >> > I tested the following code and wanted to get the message of "oval2
        >> > got hit" if I click the red one. But I always got "oval1 got hit".
        >> > from Tkinter import *
        >> > root=Tk()
        >> > canvas=Canvas(r oot,width=100,h eight=100)
        >> > canvas.pack()
        >> > a=canvas.create _oval(10,10,20, 20,tags='oval1' ,fill='blue')
        >> > b=canvas.create _oval(50,50,80, 80,tags='oval2' ,fill='red')
        >> > def myEvent(event):
        >> > if a:[/color]
        >>
        >> Here is your problem. a is a name, bound to some value. So - it is true,
        >> as python semantics are that way. It would not be true if it was e.g.
        >>
        >> False, [], {}, None, ""
        >>
        >>
        >> What you want instead is something like
        >>
        >> if event.source == a:
        >> ...
        >>
        >> Please note that I don't know what event actually looks like in Tkinter,
        >> so check the docs what actually gets passed to you.[/color]
        >
        > got AttributeError: Event instance has no attribute 'source'[/color]

        Note that Diez said /something/ /like/ /event.source/. The source is
        actually called widget -- but that doesn't help as it denotes the canvas as
        a whole, not the individual shape.

        The following should work if you want one handler for all shapes:

        def handler(event):
        print event.widget.ge ttags("current" )[0], "got hit"
        canvas.tag_bind ('oval1', '<Button>', handler)
        canvas.tag_bind ('oval2', '<Button>', handler)

        I prefer one handler per shape:

        def make_handler(me ssage):
        def handler(event):
        print message
        return handler

        canvas.tag_bind ('oval1', '<Button>', make_handler("o val 1 got hit"))
        canvas.tag_bind ('oval2', '<Button>', make_handler("o val 2 got hit"))

        Peter

        Comment

        • Diez B. Roggisch

          #5
          Re: oval

          >>[color=blue][color=green]
          >>What you want instead is something like
          >>
          >>if event.source == a:
          >> ...
          >>
          >>Please note that I don't know what event actually looks like in Tkinter,
          >>so check the docs what actually gets passed to you.[/color]
          >
          >
          > got AttributeError: Event instance has no attribute 'source'[/color]

          As I said: I don'k _know_ how event actually looks like,a s I'm not a
          Tkinter-expert, but I've had plenty of projects involving other
          GUI-Toolkits, which work all the same in this manner. After all the
          whole purposs of the event parameter is to inform you about the cause
          for that event - in this case the pressing of a MB on a specific canvas
          element.

          But if you'd take it on you to consult the documentation as I asked you
          to do, I'm pretty sure you find the proper attribute.


          Regards,

          Diez

          Comment

          • Ben Bush

            #6
            Re: oval

            On 12/4/05, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=blue][color=green][color=darkred]
            > >>
            > >>What you want instead is something like
            > >>
            > >>if event.source == a:
            > >> ...
            > >>
            > >>Please note that I don't know what event actually looks like in Tkinter,
            > >>so check the docs what actually gets passed to you.[/color]
            > >
            > >
            > > got AttributeError: Event instance has no attribute 'source'[/color]
            >
            > As I said: I don'k _know_ how event actually looks like,a s I'm not a
            > Tkinter-expert, but I've had plenty of projects involving other
            > GUI-Toolkits, which work all the same in this manner. After all the
            > whole purposs of the event parameter is to inform you about the cause
            > for that event - in this case the pressing of a MB on a specific canvas
            > element.
            >
            > But if you'd take it on you to consult the documentation as I asked you
            > to do, I'm pretty sure you find the proper attribute.
            >
            >
            > Regards,
            >
            > Diez
            > --
            > http://mail.python.org/mailman/listinfo/python-list
            >[/color]
            thanks anyway. The python manual for event is very brief and it is
            hard for me to understand.


            --
            Ben Bush

            Comment

            • Ben Bush

              #7
              Re: oval

              On 12/4/05, Peter Otten <__peter__@web. de> wrote:[color=blue]
              > Ben Bush wrote:
              >[color=green]
              > > On 12/4/05, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=darkred]
              > >> Ben Bush wrote:
              > >> > I tested the following code and wanted to get the message of "oval2
              > >> > got hit" if I click the red one. But I always got "oval1 got hit".
              > >> > from Tkinter import *
              > >> > root=Tk()
              > >> > canvas=Canvas(r oot,width=100,h eight=100)
              > >> > canvas.pack()
              > >> > a=canvas.create _oval(10,10,20, 20,tags='oval1' ,fill='blue')
              > >> > b=canvas.create _oval(50,50,80, 80,tags='oval2' ,fill='red')
              > >> > def myEvent(event):
              > >> > if a:
              > >>
              > >> Here is your problem. a is a name, bound to some value. So - it is true,
              > >> as python semantics are that way. It would not be true if it was e.g.
              > >>
              > >> False, [], {}, None, ""
              > >>
              > >>
              > >> What you want instead is something like
              > >>
              > >> if event.source == a:
              > >> ...
              > >>
              > >> Please note that I don't know what event actually looks like in Tkinter,
              > >> so check the docs what actually gets passed to you.[/color]
              > >
              > > got AttributeError: Event instance has no attribute 'source'[/color]
              >
              > Note that Diez said /something/ /like/ /event.source/. The source is
              > actually called widget -- but that doesn't help as it denotes the canvas as
              > a whole, not the individual shape.
              >
              > The following should work if you want one handler for all shapes:
              >
              > def handler(event):
              > print event.widget.ge ttags("current" )[0], "got hit"
              > canvas.tag_bind ('oval1', '<Button>', handler)
              > canvas.tag_bind ('oval2', '<Button>', handler)
              >
              > I prefer one handler per shape:
              >
              > def make_handler(me ssage):
              > def handler(event):
              > print message
              > return handler
              >
              > canvas.tag_bind ('oval1', '<Button>', make_handler("o val 1 got hit"))
              > canvas.tag_bind ('oval2', '<Button>', make_handler("o val 2 got hit"))
              >
              > Peter
              >
              > --
              > http://mail.python.org/mailman/listinfo/python-list
              >[/color]
              Peter,
              It works. Thanks!
              is there any good material to read if I want to improve my
              understanding of working on interactive ways of dealing with shapes
              on the TKinter?
              --
              Ben Bush

              Comment

              • Peter Otten

                #8
                Re: oval

                Ben Bush wrote:
                [color=blue]
                > is there any good material to read if I want to improve my
                > understanding of working on interactive ways of dealing with shapes
                > on the TKinter?[/color]

                See http://wiki.python.org/moin/TkInter for a list of references.
                For me John Shipman's Tkinter Reference is normally sufficient.

                Peter

                Comment

                Working...