import tkinter as tk# 文件选择框的库import tkinter.filedialog as tkf# 智能语音库import pyttsx3# 2、实现选择书籍功能def select_file():global book_textbook_text = ''# 以选择文件对话框path = tkf.askopenfilename()with open(path, 'r', encoding='utf-8') as file:book_text = file.read()# 把读取的内容插入展示text.insert('end', book_text)print(book_text)# 实现听书功能 —— 智能语音库def broad_text():# 初始化book = pyttsx3.init()book.say(book_text)book.runAndWait()# 1、GUI图形用户界面 —— 设置整体布局# 创建图形界面window = tk.Tk()# 设置标题window.title('懒人听书系统')# 修改大小window.geometry('640x480')# 文本框text = tk.Text(window)# 设置GUI的绝对布局 x、y为0 —— 最左边text.place(x=20, y=20, width=600, height=400)# 两个选择按钮open_button = tk.Button(window, text='选择书籍', command=select_file())open_button.place(x=100, y=440, width=150, height=30)open_button = tk.Button(window, text='开始听书', command=broad_text())open_button.place(x=350, y=440, width=150, height=30)# 实现window.mainloop()
运行结果:
如果觉得《python实现懒人听书》对你有帮助,请点赞、收藏,并留下你的观点哦!