【问题标题】:Want to get a grip of this pyautogui command想要掌握这个 pyautogui 命令
【发布时间】:2020-05-09 19:47:37
【问题描述】:

好吧,我几乎只是复制了这段代码,它获取了光标的xy 位置,并且“我想问一下每行命令的作用,以便掌握它。

提前致谢!

print('Type Ctrl-C to stop the program')


try:
    while True:
        x, y = pg.position() 
        coordinates = 'X: ' + str(x).ljust(4) + ' Y: ' + str(y).ljust(4)


        print(coordinates, end='')
        print('\b' * len(coordinates), end='', flush = True)

except KeyboardInterrupt():
    print('\n See you next time!')

【问题讨论】:

    标签: python location pyautogui


    【解决方案1】:

    您已经定义了一个永远不会结束的 while 循环,因为 True 是普遍真理。

    之后,您使用pg.position() 获取实时光标位置,这将在此处定义的两个元组中为您提供光标在屏幕上的 x 和 y 坐标 x 和 y。您可以通过pg.size()获取您的屏幕尺寸

    然后,您将定义一个名为coordinates 的变量,其中您使用ljust() 方法返回给定最小宽度内的左对齐字符串。 str.ljust(width[, fillchar]) 如果定义了fillchar,它也会用定义的字符填充剩余的空间。

    在下一行中,您使用flush() 方法打印了坐标并再次打印了坐标长度,该方法唯一的工作是刷新内部缓冲区。 \b 用于在其前退格一个字符。

    最后,您使用的是KeyboardInterrupt(),当您尝试通过在命令行中按 ctrl+c 或 ctrl+z 来停止正在运行的程序时会引发该错误。 这是一个摘要,但为了掌握,我建议查看整个文档。

    希望对你有帮助!

    【讨论】:

    • 现在我获得了对回复发表评论的声誉限制,并感谢您的澄清!
    • 恭喜@lowy2low :)
    猜你喜欢
    • 2016-06-17
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2021-10-09
    • 2022-01-26
    相关资源
    最近更新 更多