how to use Region.onChange in pyton with jpype

Asked by wu

how to use Region.onChange in pyton with jpype
i am use jpype in pyhgon use sikuli but the code
Region.onChange(10,change) is not work will return AttributeError: 'org.sikuli.script.Screen' object has no attribute 'Region'
and simple the Region and Screen is work
ex:
   Region = (100,100,100,100) and screen.click() that will work
i want to ask the Region onChange is exists in Region or Screen Class and how to use it

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

  • by wu
Revision history for this message
wu (chouhua) said :
#1

RuntimeError: No matching overloads found for onChange in find. at native\common\jp_method.cpp:127

Revision history for this message
wu (chouhua) said :
#2

version 1.1.3

Revision history for this message
RaiMan (raimund-hocke) said :
#3

using jpype, you have to look at the Java API and use the classes, methods, ... as defined there.

The SikuliX Jython API in some places differ from the Java API.

The Jython wrapper is defined in Lib/sikuli/Sikuli.py. Looking into it might help to understand the one or other problem and find a solution.

BTW: I decided to use py4j as a python-to-java bridge: my project in an early stage: https://github.com/RaiMan/sikulix4python

Revision history for this message
wu (chouhua) said :
#4

thx RaiMan i use jpype is not install jython but i go to C:\AppData\Roaming\Sikulix\Lib\sikuli look Sikuli.py there is not have

onChange this Class or def and Region.py and Screen.py is not have too and i go to there

http://doc.sikuli.org/javadoc/org/sikuli/script/Region.html

look for onChange is true in Region but i don't sure there is true

and my another question [how to use config this question have solution?]

i will try this py4j thx

Revision history for this message
wu (chouhua) said :
#5

@RaiMan (raimund-hocke) i look the sikuli4python i just feel it good but it not have onchange class in there i hope the sikuli4python will be good and finally finish

Revision history for this message
wu (chouhua) said :
#6

and how to use java -jar path-to/sikulixapi.jar -p i try path change my sikulixapi path :java -jar C:\Users\Desktop\sikulixapi.jar -p
and it appear the ERROR title IDE not useable and content is Neither jython nor jruby available ide not yet useable with javascript only Please consult the docs for a solution

Revision history for this message
RaiMan (raimund-hocke) said :
#7

Sorry, I did not want you to use sikulix4python (very early stage), was only for information, that I am working on a solution for Python.

You should stay with jpype for no, if it works for you.

onChange(): look here for a Java example:
https://sikulix-2014.readthedocs.io/en/latest/region.html#observing-visual-events-in-a-region

The actual JavaDocs are here:
https://raiman.github.io/SikuliX1/javadocs/index.html

I really recommend to use SikuliX version 1.1.4, since I do not support versions up to 1.1.3 anymore for such special cases as yours.

http://sikulix.com

Revision history for this message
wu (chouhua) said :
#8

thx RaiMan but i see the https://raiman.github.io/SikuliX1/javadocs/index.html the Class Region sure have the method onChange

there is onchange Description:

public java.lang.String onChange​(java.lang.Object observer)

a subsequently started observer in this region should wait for changes in the region and notify the given observer about this event

minimum size of changes used: Settings.ObserveMinChangedPixels for details about the observe event handler: ObserverCallBack

for details about APPEAR/VANISH/CHANGE events: ObserveEvent

Parameters:
observer - ObserverCallBack
Returns:
the event's name
so i have no idea to use onchange in jpype and i will try 1.1.4 to do my work will if you have good slove with jpype onchange plz tell me thx

Revision history for this message
RaiMan (raimund-hocke) said :
#9

You have to work along the Java outline:

// one has to combine observed event and its handler
// overriding the appropriate method
someRegion.onChange(
        new ObserverCallBack() {
                @Override
                public void appeared(ObserveEvent event) {
                        // here goes your handler code
                }
        }
);
// run observation in foreground for 10 seconds
someRegion.observe(10)

Revision history for this message
wu (chouhua) said :
#10

thx RaiMan i still no idea this code because this code language is java and how i do this in python language i use cpython and the orignal ide with python

Revision history for this message
RaiMan (raimund-hocke) said :
#11

uuups, ok ;-)

... but if you want to work with jpype against a Java API, you need some basic knowledge about how Java classes/objects/methods have to be used.

I have no time to check: it might be possible, that the above Java-typical callback construct cannot be implemented with jpype directly and might need a Python workaround.

Revision history for this message
wu (chouhua) said :
#12

i know thx RaiMan so if i want to use onchange in java api should to override the onchange? but why sikulix ide can use and don't override is not same? i just a beginner so... because i know ide is source in java and use python language i use in python to Region.Methods find onchange it show the onchange it not find 。that ok i will find another slove thx

Revision history for this message
wu (chouhua) said :
#13

Thanks RaiMan, that solved my question.

Revision history for this message
wu (chouhua) said :
#14

look like i find onchange but it another error

jpype._jexception.java.lang.ClassCastExceptionPyRaisable: java.lang.ClassCastException: java.lang.Long cannot be cast to org.sikuli.script.ObserverCallBack

Revision history for this message
RaiMan (raimund-hocke) said :
#15

at the Java level use:
someRegion.onChange(long, callback)

where long is the minimum size for changed pixels (default 50)

so my Java example again with 2 parameters:// one has to combine observed event and its handler
// overriding the appropriate method
someRegion.onChange(50,
        new ObserverCallBack() {
                @Override
                public void appeared(ObserveEvent event) {
                        // here goes your handler code
                }
        }
);
// run observation in foreground for 10 seconds
someRegion.observe(10)

Revision history for this message
RaiMan (raimund-hocke) said :
#16

-- but why sikulix ide can use?
It is internally implemented using Jython internals on the Python side and reflection on the Java side.

Revision history for this message
wu (chouhua) said :
#17

so the onchange is work but i need to rewrite the ObserverCallBack the function ?

Revision history for this message
wu (chouhua) said :
#18

i do something but it not finish i find onchange and can use but it not return something
my method :
   Jappeared = ObserverCallBack.appeared

def change(event):
    print('change')

class Appeared(Jappeared):
     pass
 this class can override the father Jappeared and use

someRegion.onChage(50,Appeared(change))

this can work but can not print('change') but the screen is sure change

i think is the // here goes your handler code need something? but i see the java api appeared the method is not print or return something

Revision history for this message
RaiMan (raimund-hocke) said :
#19

Nice try ;-)

IMHO:

class CallBack(ObserverCallBack):

    def changed(event): # this is what you have to overwrite
        print ("in change handler")

someRegion.onChange(50, CallBack())

Revision history for this message
wu (chouhua) said :
#20

hi RaiMan i do you told this but have some problem there is error

class CallBack(ObserverCallBack):TypeError: __new__() takes 2 positional arguments but 4 were given

and my code is
class CallBack(ObserverCallBack):

    def changed(event):
        print ("change")

someRegion.onChange(50, CallBack())

Revision history for this message
RaiMan (raimund-hocke) said :
#21

sorry, but I do not have the time to dive into that problem area.
As mentioned: I do not plan to work with jpype anyways.

Revision history for this message
wu (chouhua) said :
#22

ok thx RaiMan

Can you help with this problem?

Provide an answer of your own, or ask wu for more information if necessary.

To post a message you must log in.