你可以使用你的終端作爲「窗口」並在其中畫一個「圓圈」。作爲一個非常簡單的(和不可靠的)「計時器」,time.sleep()
函數可用於:
#!/usr/bin/env python
"""Print red circle walking randomly in the terminal."""
import random
import time
from blessings import Terminal # $ pip install blessings colorama
import colorama; colorama.init() # for Windows support (not tested)
directions = [(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]
t = Terminal()
with t.fullscreen(), t.hidden_cursor():
cur_y, cur_x = t.height // 2, t.width // 2 # center of the screen
nsteps = min(cur_y, cur_x)**2 # average distance for random walker: sqrt(N)
for _ in range(nsteps):
y, x = random.choice(directions)
cur_y += y; cur_x += x # update current coordinates
print(t.move(cur_y, cur_x) +
t.bold_red(u'\N{BLACK CIRCLE}')) # draw circle
time.sleep(6 * 0.017) # it may sleep both less and more time
print(t.clear) # clear screen
要嘗試它,代碼保存到random-walker.py
並運行它:
$ python random-walker.py
我不知道無論它在Windows上工作。
來源
2014-03-01 23:00:33
jfs
你可以發佈你的腳本?沒有樣本討論就很難談論你的代碼。你想在哪裏顯示窗口?在設備上?你能列舉更詳細的步驟嗎? –
@MylesBaker python腳本在我的電腦上運行,我需要腳本在我的電腦上顯示一個圓圈,我在終端窗口中運行腳本。理想情況下,我想製作一個窗口並繪製它。 – jmasterx
您需要選擇一個繪圖庫。請看這裏:http://stackoverflow.com/questions/326300/python-best-library-for-drawing –