1 回答

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
當(dāng)執(zhí)行某些會(huì)阻止代碼的任務(wù)時(shí),您可以使用線程來運(yùn)行代碼。(在您的代碼中,sleep(1)將阻止代碼),無論如何,這在我的電腦上運(yùn)行良好:
from pynput.mouse import Listener
import time
import threading
def task(): # this is what you want to do.
time.sleep(1) # <<< Tried to add it over here cuz it takes most of the process.
print("After sleep 1 second")
def on_move(x, y):
print('Pointer moved to {0}'.format((x, y)))
threading.Thread(target=task).start() # run some tasks here.
def on_click(x, y, button, pressed):
print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))
if not pressed:
return False
def on_scroll(x, y, dx, dy):
print('Scrolled {0}'.format((x, y)))
with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
listener.join()
添加回答
舉報(bào)