含有 调用windows本地语音播报 python多线程打包 等小技巧
点击下方地址直接下载压缩包(免费为大家提供)
软件获取地址
大家拿了软件别忘了给博主一个免费的赞,谢谢!!
解压压缩包
里面的MyAPP.exe就是软件了,names.txt可以按照你需要点名的列表自行输入,我这里就放了这么几个名字
基本软件界面
点击开始,会随机滚动名字,再按下暂停,实现点名,并将名字放在左侧已点名列表,同时进行语音播报
需要接着点名时,继续点击开始就会再次点名(不会重复)
如果需要重新点名,点击重新点名按钮,这时左侧框就会清空,重新在所有名字中点名
工程源码下载
主要代码部分,看不懂的可以看后面的部分解析
import Main_UI
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer
import sys, os
import threading
import random
import time
import copy
import multiprocessing
import win32com.client
import pythoncom
class Main_window(QtWidgets.QMainWindow, Main_UI.Ui_MainWindow):
def __init__(self, parent=None):
super(Main_window, self).__init__(parent)
self.setupUi(self)
self.state = 0 # 0停止状态 1运行状态
self.pushButton_start_stop.clicked.connect(self.start_stop)
self.pushButton_restart.clicked.connect(self.restart)
try:
file = open('names.txt', 'r', encoding='utf-8')
data = file.read().splitlines() # 去掉换行符'\n',此时每个元素还是字符串
self.name_list = [x for x in data] # for循环将每个元素从字符串转换成数字
except:
QtWidgets.QMessageBox.warning(self, '警告', '请在文件夹下放入utf-8格式的names.txt')
self.tmp_name_list = copy.deepcopy(self.name_list)
self.name_text = "小可爱"
self.state = 0
def restart(self):
self.state = 0
self.name_text = "小可爱"
self.label_name_show.setText(self.name_text)
self.tmp_name_list = copy.deepcopy(self.name_list)
self.textBrowser_name_list.clear()
def start_stop(self):
if self.state == 0:
if len(self.tmp_name_list) < 1:
QtWidgets.QMessageBox.warning(self, '警告', '已经把人名都点完了,请点击重新点名')
return
self.number_list = [i for i in range(len(self.tmp_name_list))]
random.shuffle(self.number_list)
self.state = 1
tmp_thread = threading.Thread(target=self.update_name)
tmp_thread.start()
self.pushButton_start_stop.setText("暂停")
else:
speech_thread = threading.Thread(target=self.speech_name)
speech_thread.start()
self.textBrowser_name_list.append(self.name_text)
self.state = 0
self.pushButton_start_stop.setText("开始")
self.tmp_name_list.remove(self.name_text)
# 语音播报
def speech_name(self):
pythoncom.CoInitialize()
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak(self.name_text)
pythoncom.CoUninitialize()
def update_name(self):
while 1:
for num in self.number_list:
if self.state == 1:
self.name_text = self.tmp_name_list[num]
self.label_name_show.setText(self.name_text)
time.sleep(0.02)
else:
return
if __name__ == '__main__':
multiprocessing.freeze_support()
app = QtWidgets.QApplication(sys.argv)
main_window = Main_window()
main_window.show()
sys.exit(app.exec_())
qt类继承
从UI集成class的模板,Main_UI是Main_UI.ui文件转化得到的
class Main_window(QtWidgets.QMainWindow, Main_UI.Ui_MainWindow):
def __init__(self, parent=None):
super(Main_window, self).__init__(parent)
self.setupUi(self)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main_window = Main_window()
main_window.show()
sys.exit(app.exec_())
读取txt获取名字并转化为list
file = open('names.txt', 'r', encoding='utf-8')
data = file.read().splitlines() # 去掉换行符'\n',此时每个元素还是字符串
self.name_list = [x for x in data] # for循环将每个元素从字符串转换成数字
生成随机序列
利用random生成随机序列,保证点名的随机性
self.number_list = [i for i in range(len(self.tmp_name_list))]
random.shuffle(self.number_list)
python多线程
我两次用到了多线程,名字的循环显示、语音播报
import threading
tmp_thread = threading.Thread(target=self.update_name)
tmp_thread.start()
调用windows本地语音播报
在多线程下跑,放置阻塞,在线程前后分别加上pythoncom.CoInitialize()和pythoncom.CoUninitialize()处理端口进程,不然在多线程情况下就会报错
利用win32com进行语音播报
我这个方式觉得是比较舒服的调用windows语音合成的,可以作为模板套用
import win32com.client
import pythoncom
speech_thread = threading.Thread(target=self.speech_name)
speech_thread.start()
# 语音播报
def speech_name(self):
pythoncom.CoInitialize()
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak(self.name_text)
pythoncom.CoUninitialize()
需要打包的python多线程程序,需要引入multiprocessing,并且像我这样加上这一句multiprocessing.freeze_support(),当然如果你加了打包还是报错,那就不是代码的问题,可能是你此时环境设置有问题,建议换一下虚拟环境试试
import multiprocessing
if __name__ == '__main__':
multiprocessing.freeze_support()