Sikuli 1.1从现有区域创建新区域

2024-07-05 09:48:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图重用预定义区域,但在使用sikuli.setW()将其分配给一个新变量时出现了Nonetype错误。这是我的密码:

import math
import sikuli

self.screen_reg = sikuli.Screen(0)
self.monitor_reg = self.screen_reg

self.leftreg = sikuli.Region(
    self.monitor_reg.x,
    self.monitor_reg.y,
    int(math.floor(self.monitor_reg.w/2)),
    self.monitor_reg.h)

self.rightreg = sikuli.Region(
    self.monitor_reg.x + int(math.floor(self.monitor_reg.w/2)),
    self.monitor_reg.y,
    int(math.floor(self.monitor_reg.w/2)),
    self.monitor_reg.h)

self.leftreg.highlight(3) <=== working

self.quarter = self.leftreg.setW(int(math.floor(self.leftreg.w/2)))

self.quarter.highlight(3) <====== didnt work; 

error: NoneType object has no attribute highlight

如果I print type(quarter),则返回NoneType。你知道吗

如果我把它改成:

self.leftreg.highlight(3)
self.leftreg.setW(int(math.floor(self.leftreg.w/2)))
self.leftreg.highlight(3)

很好用。我错过了什么?谢谢你的帮助。你知道吗


Tags: importselfmathregscreenregionmonitorint
1条回答
网友
1楼 · 发布于 2024-07-05 09:48:28

>;我错过了什么?

对象方法不能有返回类型。你知道吗

以下是Sikuli source code的节选

  public void setW(int W) {
    w = W > 1 ? W : 1;
    initScreen(null);
  }

setW的返回类型是void。也就是说,它不返回任何内容,而您希望它返回一个区域。你知道吗

正确的方法是:

self.quarter = Region(self.leftreg) # this constructs a new region
self.quarter.setW(int(math.floor(self.leftreg.w/2)))  # and this resizes it

相关问题 更多 >