簡體   English   中英

僅在Keyboardinterrutps之后累積回溯

[英]Traceback accumulation only after Keyboardinterrutps

這是我的代碼:

import autopy
import time
import math
width, height = 400, 400
a, b = 200, 200
r = 150
def drawCicle():
    for x in range(0, 3):
        for angle in range(0, 360, 1):
            x = r * math.sin(math.radians(angle)) + a
            y = r * math.cos(math.radians(angle)) + b
            autopy.mouse.move(int(x),int(y))
            time.sleep(0.002)
def mouseMove():
    counter = 0
    while counter < 4:
        drawCicle()
        counter += 1
    else:
        print('Drawing ' + str(counter) + ' circles')
        print('moving once more in 10 seconds...')
        counter = 0
        time.sleep(10)
        mouseMove()

if __name__ == "__main__":
    mouseMove()

奇怪的是,代碼運行得很好。 只有在使用KeyboardInterrupt中斷循環后,我才會獲得回溯,這樣從每個循環中溢出累積的回溯,如下所示:

Traceback (most recent call last):
  File "test.py", line 27, in <module>
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 23, in mouseMove
    time.sleep(10)
KeyboardInterrupt

僅當我手動破壞代碼時,這種情況才會發生,任何人都可以闡明我所忽略的最佳實踐嗎?

如果要立即查看打印語句的輸出,請使用flush=True

輸出是否被緩沖通常由file決定,但是如果flush關鍵字參數為true,則將強制刷新流。


def mouseMove():
    counter = 0
    while counter < 4:
        drawCicle()
        counter += 1
    else:
        print('Drawing ' + str(counter) + ' circles', flush=True)
        print('moving once more in 10 seconds...', flush=True)
        counter = 0
        time.sleep(10)
        mouseMove()

僅當您退出程序並出現異常時,才打印異常回溯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM