初學python寫了個回圈倒計時腳本,有暫停(按鍵盤空格)、重置功能(按鍵盤回車)。兩個執行緒,一個執行緒回圈倒計時并turtle繪制數碼管圖,一個執行緒負責監聽鍵盤按鍵,共享類action屬性去實作定時重置功能。但是腳本能正常倒計時,但是一暫停(暫停后等一段時間)或計時結束(計時結束后我想讓畫面保持靜止狀態,并監聽是否有回車可重置倒計時)界面就會未回應。麻煩大佬們幫我看看。
另外還有指定時間段響鈴功能,讀取同一目錄下mp3檔案,也是通過添加執行緒去響鈴,保證倒計時不受鈴聲影響。通過添加字典

import turtle
import time
import threading
import pygame, os, re, sys
from mutagen.mp3 import MP3
from pynput.keyboard import Key,Listener
class mainApp():
def __init__(self, total_time, bell_set):
self.total_time = total_time
self.bell_set = bell_set
self.action_space = True
self.action_enter = False
self.action_esc = True
self.script_path = os.path.abspath(os.path.dirname(__file__))#os.path.dirname(os.path.realpath(sys.executable)) # #
print(self.script_path)
for file_name in os.listdir(self.script_path):
if re.match(".*(.mp3)$", file_name):
self.musicPath = self.script_path + "\\" + file_name
self.audio = MP3(self.musicPath)
pygame.mixer.init() # 初始化所有引入的模塊
self.lock = threading.Lock()
def rang(self, num):
for i in range(num):
pygame.mixer.music.load(self.musicPath) # 載入音樂,音樂可以是 ogg、mp3 等格式
pygame.mixer.music.play() # 播放載入的音樂
time.sleep(int(self.audio.info.length)/2) # 獲取每一首歌曲的時長,使程式存活的時長等于歌曲時長
def rang_thread(self, old_minuts, old_second, ring_time):
num = ring_time[old_minuts + ':' + old_second]
threading.Thread(target=self.rang, args=(num,)).start()
def drawLine(self, draw):
turtle.penup()
turtle.pendown() if draw else turtle.penup()
turtle.fd(130)
turtle.right(90)
def drawDigit(self, digit):
self.drawLine(True) if digit in [2,3,4,5,6,8,9] else self.drawLine(False)
self.drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else self.drawLine(False)
self.drawLine(True) if digit in [0,2,3,5,6,8,9] else self.drawLine(False)
self.drawLine(True) if digit in [0,2,6,8] else self.drawLine(False)
turtle.left(90)
self.drawLine(True) if digit in [0,4,5,6,8,9] else self.drawLine(False)
self.drawLine(True) if digit in [0,2,3,5,6,7,8,9] else self.drawLine(False)
self.drawLine(True) if digit in [0,1,2,3,4,7,8,9] else self.drawLine(False)
turtle.left(180)
turtle.penup()
turtle.fd(50)
def drawTime(self, localtime):
turtle.pencolor("green")
for i in localtime:
if i == ':':
turtle.goto(0, 80)
turtle.pendown()
turtle.circle(3)
turtle.penup()
turtle.goto(0, -80)
turtle.pendown()
turtle.circle(3)
turtle.penup()
turtle.goto(0, 0)
# turtle.write(':',font=("Arial",18,"normal"))
turtle.fd(50)
else:
self.drawDigit(eval(i))
def drawTime2(self, localtime):
for i in localtime:
self.drawDigit(eval(i))
def reset(self, x,y):
turtle.goto(x,y)
turtle.pencolor("black")
self.drawTime2("88")
turtle.goto(x,y)
def timing_process(self):
turtle.setup(810,410)
turtle.screensize(800,400, "black")
turtle.penup()
turtle.speed(0)
turtle.Turtle().screen.delay(0)
turtle.fd(-360)
turtle.pensize(10)
self.drawTime(self.total_time)
turtle.hideturtle()
old_minuts = self.total_time.split(':')[0]
old_second = self.total_time.split(':')[-1]
ring_time = self.bell_set
sys_old_second = time.strftime('%S', time.localtime())
while self.action_esc:
if self.action_enter:
turtle.tracer(0)
self.reset(50,0)
self.reset(-360,0)
self.drawTime(self.total_time)
old_minuts = self.total_time.split(':')[0]
old_second = self.total_time.split(':')[-1]
turtle.update()
self.action_enter = False
self.action_space = False
continue
if self.action_space:
# sys_old_second = time.strftime('%S', time.localtime())
pass
else:
sys_old_second = time.strftime('%S', time.localtime())
continue
if int(old_second) == 0 and int(old_minuts) != 0:
#根據系統時間進行延時
sys_now_second = time.strftime('%S', time.localtime())
if sys_now_second == sys_old_second:
continue
else:
sys_old_second = sys_now_second
execute = True
turtle.tracer(0)
now_minuts = (str(int(old_minuts)-1) if len(str(int(old_minuts)-1)) == 2 else '0' + str(int(old_minuts)-1))
self.reset(-360,0)
turtle.pencolor("green")
self.drawTime2(now_minuts)
self.reset(50,0)
turtle.pencolor("green")
self.drawTime2("59")
old_second = "59"
old_minuts = now_minuts
turtle.update()
continue
elif int(old_second) == 0 and int(old_minuts) == 0:
continue
#根據系統時間進行延時
sys_now_second = time.strftime('%S', time.localtime())
if sys_now_second == sys_old_second:
continue
else:
sys_old_second = sys_now_second
execute = True
turtle.tracer(0)
self.reset(50, 0)
turtle.pencolor("green")
now_second = str(int(old_second)-1) if len(str(int(old_second)-1)) == 2 else '0' + str(int(old_second)-1)
self.drawTime2(now_second)
turtle.update()
old_second = now_second
# 避免系統1秒鐘內創建過多無用執行緒
if execute and ring_time:
if old_minuts + ':' + old_second in ring_time.keys():
self.rang_thread(old_minuts, old_second, ring_time)
execute = False
# 鍵盤監聽
def on_press(self, key):
if key == Key.enter:
self.lock.acquire()
try:
self.action_enter = True
finally:
self.lock.release()
if key == key.space:
self.lock.acquire()
try:
self.action_space = not self.action_space
finally:
self.lock.release()
if key == key.esc:
self.lock.acquire()
try:
self.action_esc = False
finally:
self.lock.release()
#監聽鍵盤按鍵
def key_listen(self):
with Listener(on_press=self.on_press) as listener:
listener.join()
obj = mainApp('01:00', None)
t_timing = threading.Thread(target=obj.timing_process)
t_listen = threading.Thread(target=obj.key_listen)
t_timing.start()
t_listen.start()
if obj.action_esc:
pass
else:
t_listen.stop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/54208.html
下一篇:Python如何處理配速中的秒
