python实现音乐定时开关,模拟上下课铃声(含程序打包微小exe文件方法介绍) 您所在的位置:网站首页 模拟学校上课铃 python实现音乐定时开关,模拟上下课铃声(含程序打包微小exe文件方法介绍)

python实现音乐定时开关,模拟上下课铃声(含程序打包微小exe文件方法介绍)

2023-08-15 00:35| 来源: 网络整理| 查看: 265

本文分两部分,先介绍python实现模拟上下课定时铃声播放与关闭,再讲python程序打包成小型exe文件。

一、python实现模拟上下课定时铃声播放与关闭 不说废话,完整代码奉上。

import sys import time import datetime import pygame # 实现闹钟功能 def alert(): music = input("请设置铃声(路径): ") # D:\离骚-易烊千玺.mp3 study_time = int(input("请设置上课时长(分钟): ")) rest_time = int(input("请设置休息时长(分钟): ")) start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) while True: now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) sys.stdout.write('上课中:'+now + '\r') sys.stdout.flush() time.sleep(1) t1 = datetime.datetime.strptime(start, '%Y-%m-%d %H:%M:%S') # 上课时间 t2 = datetime.datetime.strptime(now, '%Y-%m-%d %H:%M:%S') # 下课时间 if (t2 - t1).seconds == (study_time * 60): pygame.mixer.init() # 初始化 pygame.mixer.music.load(music) # 加载音乐 pygame.mixer.music.play() # 播放音乐 while 1: now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) sys.stdout.write('下课中:'+now + '\r') sys.stdout.flush() if pygame.mixer.music.get_busy(): # 音乐放 if ((datetime.datetime.strptime(now, '%Y-%m-%d %H:%M:%S') - t2).seconds


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有