简体   繁体   中英

Looping trouble in Sikuli/Python

Not sure where I'm going wrong:

mm = list(r.findAll(rButton))# find all rButtons on main screen
print len(mm) #check how many are detected
for x in range(0,len(mm)):
    r.click(mm[x])
    if(not r.exists(rButtonDisabled)):
        print "this is a test"
        r.wait(BeginTask,FOREVER)
        r.click(BeginTask)
        r.wait(rButton,FOREVER)
    else: click(Cancel)

There are 2 screens. Let's call them main screen and screen2. On main screen there are identical buttons, rButton. I want to find all visible rButtons and then start clicking them. Ideally I want it to click on first rButton, which takes it to screen2, if the button on screen2 is disabled, click on cancel which moves us back to main screen, then go to the second rButton on main screen, which again takes us to screen2. Depending on rButtons on main screen, buttons on screen2 can be either disabled or enabled.

My code isn't working to that effect. Not sure where I'm going wrong.

I'm not sure how you've defined Region 'r', but as a default, Sikuli won't search outside the screen that is native to the OS. You need to first make sikuli find the other screen, then define the bounds of that screen.

As it appears now, you're searching Region 'r' no matter what screen you intended... You should define the two screens separately, or Sikuli won't know to switch screens to look for the button you want. For example, you can use the Screen class to define which screen is which--

numScreens = getNumberScreens()
r = SCREEN #in all caps, this is the reserve word for the whole screen native to the OS
    #can also use r = Screen(0)
if numScreens > 0 #make sure second screen was counted by sikuli
    r2 = Screen(1).getBounds() 
else: raise the appropriate error

#Here's your code with some adjustments for multiple monitors
#on main screen
mm = list(r.findAll(rButton))# find all rButtons on main screen
print len(mm) #check how many are detected
for x in range(0,len(mm)):
    r.click(mm[x])
    #on secondary screen
    if(not r2.exists(rButtonDisabled)):
        print "this is a test"
        r2.wait(BeginTask,FOREVER)
        r2.click(BeginTask)
        #back to main screen
        r.wait(rButton,FOREVER)
    #click the cancel button on secondary screen
    else: r2.click(Cancel) # <-- the defining region was missing here in your original code

Here's the Sikuli documentation on multi-monitor environments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM