首页 > 解决方案 > return didn't return. Windows api

问题描述

Here's my code. When I run it the return statement doesn't return anything. Anyone know why?

import win32api as win
import time
import sys
def mouse_path(run):
    path = []
    while time.clock() < run:
        cords = (win.GetCursorPos())
        time.sleep(.1)
        path.append(cords)
    return path

mouse_path(10)

标签: pythonwinapireturn

解决方案


看起来您是从 .py 文件而不是在交互模式下运行它。除非在交互模式下,否则不会显示从return语句返回的对象。尝试将返回的值包装在print语句中。

import win32api as win
import time
import sys
def mouse_path(run):
    path = []
    while time.clock() < run:
        cords = (win.GetCursorPos())
        time.sleep(.1)
        path.append(cords)
    return path

print(mouse_path(10))

我运行它并将其打印到屏幕上:

[(514, 469), (545, 474), (606, 465), (654, 444), (705, 430), (754, 425), (795, 423), 
 (825, 426), (821, 443), (794, 466), (774, 472), (733, 468), (679, 421), (622, 379), 
 (578, 330), (540, 305), (489, 277), (442, 284), (424, 310), (417, 323), (425, 346), 
 (460, 375), (512, 417), (563, 422), (610, 377), (677, 317), (733, 291), (787, 298), 
 (817, 328), (824, 368), (823, 401), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), (812, 419), 
 (812, 419)]

推荐阅读