win32api.SetCursorPos() question

G

Gary Richardson

I'm trying to use win32api.SetCursorPos() to position the cursor in a
Tkinter canvas window. I.e.:

from Tkinter import *
import win32api
root = Tk()
canvas = Canvas(root, width=400, height=300, bg='white')
canvas.pack()
win32api.SetCursorPos(100,100)
root.mainloop()

But this code produces:

Traceback (most recent call last):
File
"C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 301, in RunScript
exec codeObject in __main__.__dict__
File "C:\My Documents\Python\Ascii\Script4.py", line 6, in ?
win32api.SetCursorPos(100,100)
TypeError: SetCursorPos() takes exactly 1 argument (2 given)

Am I using this function incorrectly? A search on Google didn't turn up much
but I did find one bit of code in which it was used in this manner.

I suspect there may be other problems with this approach so any further
comments will be appreciated also. I'm using ActivePython 2.2.2 build 224 on
Win98SE.

Thanks,
Gary Richardson
 
V

vincent wehren

Gary said:
I'm trying to use win32api.SetCursorPos() to position the cursor in a
Tkinter canvas window. I.e.:

from Tkinter import *
import win32api
root = Tk()
canvas = Canvas(root, width=400, height=300, bg='white')
canvas.pack()
win32api.SetCursorPos(100,100)
root.mainloop()

But this code produces:

Traceback (most recent call last):
File
"C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 301, in RunScript
exec codeObject in __main__.__dict__
File "C:\My Documents\Python\Ascii\Script4.py", line 6, in ?
win32api.SetCursorPos(100,100)
TypeError: SetCursorPos() takes exactly 1 argument (2 given)

Am I using this function incorrectly? A search on Google didn't turn up much
but I did find one bit of code in which it was used in this manner.

I suspect there may be other problems with this approach so any further
comments will be appreciated also. I'm using ActivePython 2.2.2 build 224 on
Win98SE.

Different then what the docs say, this function seems to want a tuple as
argument. See if win32api.SetCursorPos((100,100)) does the trick.
 
G

Gary Richardson

vincent wehren said:
Different then what the docs say, this function seems to want a tuple as
argument. See if win32api.SetCursorPos((100,100)) does the trick.

I tried that. It doesn't produce an error but no cursor appears.
 
H

Heiko Wundram

Am Samstag, 18. September 2004 22:41 schrieb Gary Richardson:
I tried that. It doesn't produce an error but no cursor appears.

I don't know much about the win32-api, but SetCursorPos sounds like a
text-console command, not like something that has to do with windows.
Funtions that operate on a window take a HWND (window handle), but this
function doesn't, so this certainly sounds like text-console.

Now, why are you trying to position the cursor in a Tk Window anyway? The
Tk-Window is no text console, it's just a plain window on the screen, and you
can draw in the window using the normal Tk drawing primitives.

Maybe, if you post some more info on what you're trying to do, can we help you
better...

Heiko.
 
G

Gary Richardson

Heiko Wundram said:
Am Samstag, 18. September 2004 22:41 schrieb Gary Richardson:

I don't know much about the win32-api, but SetCursorPos sounds like a
text-console command, not like something that has to do with windows.
Funtions that operate on a window take a HWND (window handle), but this
function doesn't, so this certainly sounds like text-console.

Now, why are you trying to position the cursor in a Tk Window anyway? The
Tk-Window is no text console, it's just a plain window on the screen, and you
can draw in the window using the normal Tk drawing primitives.

Maybe, if you post some more info on what you're trying to do, can we help you
better...

Heiko.

Heiko,

Thank you for your reply. It encouraged me to read the MSVS documentation on
cursors a little more closely and to try a few things. I found that the
win32api function GetCursorPos() returns the position of the cursor with
respect to the computer screen so assuming that SetCursorPos() uses the same
coordinate system, I would have to add to my window coordinates the position
of the canvas with respect to the screen. I then took another look at
Vincent Wehren's and DogWalker's (direct email) suggestion that I pass
SetCursorPos() a tuple instead of a pair of values and found that did indeed
work. The cursor was there, it just wasn't where I expected it to be.

I'm using a Tk Canvas window to display various objects, one of which can be
a block of text entered from the keyboard. The user positions the cursor to
the desired position and begins typing, terminating the entry by pressing
the Esc key. From that point the text block can be manipulated (moved,
copied or deleted) by means of the mouse. As it is now, during text entry,
there is no indication of where the next character is to be entered. So I
thought it would be nice if I could indicate the insertion point as is usual
with text entry programs, editors, etc.

Thanks for all the help.

Gary
 
F

Fredrik Lundh

Gary said:
I'm using a Tk Canvas window to display various objects, one of which can be
a block of text entered from the keyboard. The user positions the cursor to
the desired position and begins typing, terminating the entry by pressing
the Esc key. From that point the text block can be manipulated (moved,
copied or deleted) by means of the mouse. As it is now, during text entry,
there is no indication of where the next character is to be entered. So I
thought it would be nice if I could indicate the insertion point as is usual
with text entry programs, editors, etc.

that's what the icursor method is there for. see:

http://effbot.org/zone/editing-canvas-text-items.htm

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top