get root.winfo_pointerxy()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

get root.winfo_pointerxy()

luofeiyu
[code]
from Tkinter import *
 
root = Tk()
root.title('Simple Plot - Version 1')
 
canvas = Canvas(root, width=450, height=300, bg = 'white')
canvas.pack()
Button(root, text='Quit', command=root.quit).pack()
 
canvas.create_line(100,250,400,250, width=2)
canvas.create_line(100,250,100,50,  width=2)
 
for i in range(11):
    x = 100 + (i * 30)
    canvas.create_line(x,250,x,245, width=2)
    #canvas.create_text(x,320, text='%d'% (10*i), anchor=N)
 
for i in range(6):
    y = 250 - (i * 40)
    canvas.create_line(100,y,105,y, width=2)
    canvas.create_text(96,y, text='%5.1f'% (50.*i), anchor=E)
 
for x,y in [(12, 56), (20, 94), (33, 98), (45, 120), (61, 180),
            (75, 160), (98, 223)]:
    x = 100 + 3*x
    y = 250 - (4*y)/5
    canvas.create_oval(x-6,y-6,x+6,y+6, width=1,
                       outline='black', fill='SkyBlue2')


def  myprint():
    print  root.winfo_pointerxy()

canvas.bind("<Button-1>",myprint)
 
root.mainloop()
[/code]

when  i  click my left  mouse,there is a output:

 File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
TypeError: myprint() takes no arguments (1 given)

what's wrong?

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: get root.winfo_pointerxy()

Cameron Laird-2
On Thu, Sep 01, 2011 at 10:37:31AM -0400, Douglas S. Blank wrote:
                        .
                        .
                        .

> On 09/01/2011 10:21 AM, ������� wrote:
> >def  myprint():
> >     print  root.winfo_pointerxy()
> >
> >canvas.bind("<Button-1>",myprint)
>
> When you bind a function to the canvas, it is expecting a function that
> takes an argument (which is probably the object to which the binding  is
> bound).
>
> So, you could just allow myprint to take an argument, and ignore it:
>
> def  myprint(arg):
>      print  root.winfo_pointerxy()
                        .
                        .
                        .
I entirely agree with the counsel Dr. Blank has provided.
While I expect the original questioner has all he needs to
move forward, I'll provide a bit more detail for the
benefit of other readers.

Let's look at "arg", "which is probably the object to
which the binding is bound".  It's not; it's the detected
*event* (from which that object can be calculated, though)
<URL: http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm >.

That's not all.  One could temporarily update myprint's
definition to be something like

  def myprint(arg):
      print "arg is '%s'." % arg
      print dir(arg)
      print root.winfo_pointerxy()

to have Python's introspection report more information
about the argument.  

And *that* isn't all, either.  If one were somehow
stranded-on-a-desert-island and unsure how many (not-
necessarily-named) arguments were arriving, one could
experiment with

    def myprint(*kw):
        print kw
          ...

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss