对不起,我对蟒蛇很陌生。
我试图将光标保持在100×100框内,但它不能做到这一点,我仍然能够在整个屏幕的t形内移动它,而不是在它中间的一个框。它似乎忽略了其中的一个变量
它应该做的只是简单地检测鼠标是否离开了100x100区域。
占位符很简单,所以我以后可以把一些东西放在那里。
pyautogui.moveTo(550,550)
while True:
mos = pyautogui.position()
print(mos[0],mos[1])
if (500 < mos[0] < 600) or (500 < mos[1] < 600) :
pass
else:
print('placeholder')
print('f')我已经开始工作了,但是我仍然不明白为什么第一个版本不能工作
pyautogui.moveTo(550,550)
while True:
mos = pyautogui.position()
print(mos[0],mos[1])
if (500 < mos[0] < 600):
pass
else:
print('placeholder')
print('f')
if (500 < mos[1] < 600):
pass
else:
print('placeholder')
print('f')发布于 2022-11-22 07:37:05
好吧,不知道,但我把它修正了,不放在它的前面
pyautogui.moveTo(550,550)
while True:
mos = pyautogui.position()
print(mos[0],mos[1])
if not (500 < mos[0] < 600) or not(500 < mos[1] < 600):
break发布于 2022-11-22 07:50:05
你的第一个版本应该是
if (500 < mos[0] < 600) and (500 < mos[1] < 600) :因为您希望光标在x和y轴的方框限制内。
将not置于这两个条件的前面,并像在最终版本中那样将逻辑反转,将得到预期的结果,因为not (not A or not B)在逻辑上与A and B相同(这称为德摩根定律)。
发布于 2022-11-22 08:04:22
我认为这种方法更容易读懂,而不是把所有的东西都压缩到if条件中。
您可能需要考虑添加睡眠以避免过度处理此过程。
最后,在这里使用"pass“没有意义,因为它允许循环的继续。而对于您的情况,您需要重新启动循环。因此,我们使用“继续”代替。
import time
import pyautogui
pyautogui.moveTo(550,550)
while True:
time.sleep(0.1)
LeftRightPos = pyautogui.position()[0]
TopDownPos = pyautogui.position()[1]
if LeftRightPos<500 or LeftRightPos>600:
print "LeftRight Out of Position"
break
elif TopDownPos<500 or TopDownPos>600:
print "TopDown Out of Position"
break
else:
continuehttps://stackoverflow.com/questions/74527424
复制相似问题