|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大佬们,是这样,我买了一本教程,书名:《Python编程快速上手》并根据其第18章按照要求抄写下了如下代码,但是发现鼠标坐标并不能显示出来。
我不理解是我对作者的讲解出了误解,还是版本不兼容这种写法。
请大家帮我看一看。
- import pyautogui
- print('Press ctrl+C to quit.')
- try:
- while True:
- x,y = pyautogui.position()
- positionStr = 'X:' + str(x).rjust(4) + 'Y:' + str(y).rjust(4)
- # 我曾尝试在这里写入最后的两行代码,但可能是因为没有设置时间,坐标打印出来以后转瞬即逝。
- except KeyboardInterrupt:
- print('\nDone.')
- print(positionStr, end='')
- print('\b' * len(positionStr), end='', flush=True)
复制代码
万分感谢。
本帖最后由 jackz007 于 2020-12-22 15:38 编辑
明白你的意图了,代码缩进的问题,显示坐标的语句被写在了循环外面,现在再测
- import pyautogui
- print('Press ctrl+C to quit.')
- try:
- x0 , y0 = pyautogui.position()
- positionStr = 'X:' + str(x0).rjust(4) + '\tY:' + str(y0).rjust(4)
- while True:
- x , y = pyautogui.position()
- if x != x0 or y != y0:
- print('\b' * (len(positionStr) + 1) , end = '', flush = True)
- positionStr = 'X:' + str(x).rjust(4) + '\tY:' + str(y).rjust(4)
- print(positionStr , end = '')
- x0 , y0 = x , y
- except KeyboardInterrupt:
- print('\nDone.')
复制代码
|
|