import tkinter as tk
# 文件选择框的库
import tkinter.filedialog as tkf
# 智能语音库
import pyttsx3
# 2、实现选择书籍功能
def select_file():
global book_text
book_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()
运行结果: