2011-03-03 7 views
5

के साथ ctypes का उपयोग करना मुझे अपनी पायथन लिपि में ctypes lib का उपयोग करने में समस्या है। यहाँ मेरी कोड (इंटरनेट पर पाया जाता है) है:ज्योथन

if __name__ == "__main__": 
    from ctypes import * 
    user32 = windll.user32 
    kernel32 = windll.kernel32 

    class RECT(Structure): 
     _fields_ = [ 
      ("left", c_ulong), 
      ("top", c_ulong), 
      ("right", c_ulong), 
      ("bottom", c_ulong)]; 

    class GUITHREADINFO(Structure): 
     _fields_ = [ 
     ("cbSize", c_ulong), 
     ("flags", c_ulong), 
     ("hwndActive", c_ulong), 
     ("hwndFocus", c_ulong), 
     ("hwndCapture", c_ulong), 
     ("hwndMenuOwner", c_ulong), 
     ("hwndMoveSize", c_ulong), 
     ("hwndCaret", c_ulong), 
     ("rcCaret", RECT) 
     ] 

    def moveCursorInCurrentWindow(x, y): 
     # Find the focussed window. 
     guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO)) 
     user32.GetGUIThreadInfo(0, byref(guiThreadInfo)) 
     focussedWindow = guiThreadInfo.hwndFocus 

     # Find the screen position of the window. 
     windowRect = RECT() 
     user32.GetWindowRect(focussedWindow, byref(windowRect)) 

     # Finally, move the cursor relative to the window. 
     user32.SetCursorPos(windowRect.left + x, windowRect.top + y) 

    if __name__ == '__main__': 
    # Quick test. 
     moveCursorInCurrentWindow(100, 100) 

पहले समस्या यह है कि अजगर ctypes नहीं पा सके तो मैं

netbeans\6.9\jython-2.5.1\Lib\ 

करने के लिए परियोजना साइट से डाउनलोड की गई फ़ाइलों की नकल की थी (हां , im NetBeans का प्रयोग करके) और फिर इसे इस त्रुटि दिखाता है: init फ़ाइल

> from ctypes import * 
> File "C:\Users\k\.netbeans\6.9\jython-2.5.1\Lib\ctypes\__init__.py", line 10, in <module> 
> from _ctypes import Union, Structure, Array 

वैसे ही जैसे कुछ त्रुटियाँ हैं o_O सहायता लोग! अभिवादन, क्रिस

+0

और त्रुटि है? – user470379

उत्तर

1

Jython अभी तक ctypes के लिए पूरा समर्थन नहीं है: http://bugs.jython.org/issue1328

आप बस CPython के लिए संकलित ctypes पुस्तकालय नहीं ले सकते हैं, और Jython में प्लग।

+0

रिकॉर्ड के लिए, ज्योथन 2.7 बीटा में, 'ctypes' समर्थन अब बेहतर हो रहा है। – Dolda2000

3

ctypes Jython 2.5.1 में समर्थित नहीं है। 2.5.2 में कुछ प्रयोगात्मक समर्थन जोड़ा गया है, लेकिन यह निश्चित रूप से कहीं भी पूर्ण नहीं है। शायद आपको JNA का उपयोग करके ज्योथन के साथ बेहतर भाग्य होगा। एक संक्षिप्त ट्यूटोरियल here है।

8

ctypes जैथन में प्रयोगात्मक और पूर्ण नहीं।

There's some experimental support for ctypes in 2.5.2 [the current version], but it's really more of a placeholder at this point.

इसके बाद वे इन काम arounds पता चलता है::

I do recommend JNA if you can modify your ctypes code. JNA is pretty close to ctypes - JNA's API apparently was significantly influenced by ctypes! JNA also seems to work well with Jython.

The other option is to use something like execnet. For execnet specifically: it allows you to pair Jython with CPython, and it does seem to work well. But its GPL license makes it a non starter for many people. There are other choices out there too.

Jython-उन शीर्षक से एक सूत्र में मेलिंग सूची से "ctypes in Jython" जिम बेकर (एक Jython committer) नवंबर 17, 2010 को लिखा था

इसके अलावा पर एक ही धागे में हम इस बात की पुष्टि आकलन है में:

I tried the ctypes module in 2.5.2rc2 recently, and found that: 1) There's no ctypes.util.find_library yet 2) ctypes.Structure doesn't support non-scalar types yet

So I agree with the "more of a placeholder" assessment. Still, it's exciting to see it getting started.

0

ठीक है, thx दोस्तों! मैं बस अपने नेटबीन को दोबारा जोड़ता हूं, अब इसका उपयोग सीपीथन है। सब कुछ काम करता है। मुझे बस user32.SetCursorPos(windowRect.left + x, windowRect.top + y) को user32.SetCursorPos(c_ulong(windowRect.left + x), c_ulong(windowRect.left + y))