我试图在函数中按下时分别保存x,y的坐标。我可以打印它们,但无法将它们保存到变量中。
这就是我得到的
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
print('{0} at {1}'.format(
'Pressed' if pressed else 'Released',
(x, y)))
if not pressed:
# Stop listener
return False
with Listener(on_click=on_click) as listener:
listener.join()
然后如何在全局范围内调用这些变量以与其他模块(pyautogui)一起使用?
答案 0 :(得分:0)
使用全局变量:
modelSchangeSubject: Subject<Model> = new Subject<Model>();
ngOnInitt() {
this.modelSchangeSubject
.subscribe((v: Model) => {
this.calculateAvailableCars()
})
}
/* Rest of controller code */
set modelBounded(v: Model) {
this.modelSchangeSubject.next(v);
}
如果您的代码变得更加复杂,则可以考虑将其包装在一个类中。
答案 1 :(得分:0)
您已经准备好了几乎所有的东西,所以我只需要添加几行。全球人并不是做事情的最好方法,但是由于该程序不太复杂,他们会做的。
down_x等的初始值并不重要,因为它们将被覆盖,但是必须存在,否则Python会抛出错误。
#if you want to delay between mouse clicking, uncomment the line below
#import time
from pynput.mouse import Listener
import pyautogui
down_x = down_y = up_x = up_y = -1
def on_click(x, y, button, pressed):
global down_x
global down_y
global up_x
global up_y
if pressed:
(down_x, down_y) = (x, y)
else:
(up_x, up_y) = (x, y)
return False
with Listener(on_click=on_click) as listener:
listener.join()
print("Mouse drag from", down_x, ",", down_y, "to", up_x, ",", up_y)
# you may wish to import the time module to make a delay
#time.sleep(1)
pyautogui.mouseDown(down_x, down_y)
#time.sleep(1)
pyautogui.mouseUp(up_x, up_y)