在shell中运行Python代码时出现NameError

时间:2017-08-20 20:23:27

标签: python python-2.7

我希望在shell中运行get_cords()函数以获取当前鼠标位置,但是,我收到错误说

Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'get_cords' is not defined

这是代码

import ImageGrab
import os
import time
import win32api, win32con

x_pad = 464
y_pad = 366


def screenGrab():
    box = (x_pad + 1, y_pad + 1, x_pad + 640, y_pad + 481)
    im = ImageGrab.grab(box)
    im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) + '.png', 'PNG')


def main():
    pass


if __name__ == '__main__':
    main()


def leftClick():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    print "Click."


def leftDown():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    time.sleep(.1)
    print "left Down"


def leftUp():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    time.sleep(.1)
    print "left Release"


def mousePos(cord):
    win32api.SetCursorPos(x_pad + cord[0], y_pad + cord[1])


def get_cords():
    x, y = win32api.GetCursorPos()
    x = x - x_pad
    y = y - y_pad
    print x, y

是的,我正在项目文件夹中运行shell。我也在运行Python 2.7.13并使用PyCharm。

1 个答案:

答案 0 :(得分:0)

不确定这个答案是否会与其他任何人相关,但对我有用的是:

>>> import file_name
>>> file_name.function_name()

和VOILA!