0

我正在尝试做一个简单的可执行文件来导入 PDF 并使用 pyttsx3 读取 PDF。由于 pyttsx3 无法暂停阅读,我使用 psutil 创建了一个小函数,该函数会杀死名为 Python.exe 的进程(可能不是理想的方式)。Easygui 的简单界面允许选择 PDF 文件、从哪个页面开始,以及在按下按钮时关闭进程(使用 psutil)。

据我了解多线程,应该有一个多线程执行的主进程;因此我认为关闭整个过程我会在用户按下按钮时停止脚本。它不起作用。线程已关闭,但主进程未关闭,因此脚本继续运行(语音继续阅读)。在我有一个工作版本后,我计划使用 pyinstaller 将其转换为 exe 文件。

代码是:

from pyttsx3 import *
from pyttsx3.drivers import sapi5
from PyPDF2 import PdfFileReader
from threading import *
from psutil import process_iter
from easygui import enterbox,fileopenbox,msgbox,choicebox

class Audiobook():

    def readbook(self,title,page=0):
        book = open(title, 'rb')
        pdfReader = PdfFileReader(book)
        pages = pdfReader.numPages
        speaker = init()

        speaker.setProperty('rate',150)
        
        for num in range(page-1,pages):
            page = pdfReader.getPage(num)
            text = page.extractText()
            speaker.say(text)
            speaker.runAndWait()
            speaker.say("You arrived to page" + str(num+1))

    def title(self):
        title = fileopenbox()
        if title:
            page = int(enterbox("From which page would you like to read? ",'Choose the page'))
        else:
            return
        self.readbook(title,page)

    def killprocess(self):
        kill = msgbox("Click here to stop reading","Close the Application?")
        if kill is not None:
            procname = 'Python.exe' #this line will be changed when converted into an executable with pyinstaller
            for proc in process_iter():
                if proc.name() == procname: 
                    proc.kill()

processcontrol = Audiobook()
bookreader = Audiobook()
process1 = Thread(target=bookreader.title)
process2 = Thread(target=processcontrol.killprocess)
process2.start()
process1.start()

知道我在做什么错吗?我欺骗了谷歌和这个网站,但没有成功。

谢谢

4

1 回答 1

1

我已经更改了 kill 过程并且只创建了一个对象(不需要两个实例)。

import pyttsx3
from pyttsx3.drivers import sapi5
from PyPDF2 import PdfFileReader
from threading import Thread
from psutil import process_iter
from easygui import enterbox,fileopenbox,msgbox,choicebox
import os
import signal

class Audiobook():

    def readbook(self,title,page=0):
        book = open(title, 'rb')
        pdfReader = PdfFileReader(book)
        pages = pdfReader.numPages
        speaker = pyttsx3.init()

        speaker.setProperty('rate',150)

        for num in range(page-1,pages):
            page = pdfReader.getPage(num)
            text = page.extractText()
            speaker.say(text)
            speaker.runAndWait()
            speaker.say("You arrived to page" + str(num+1))

    def title(self):
        title = fileopenbox(default="*.pdf",filetypes=["*.pdf"])
        if title:
            page = enterbox("From which page would you like to read? ",'Choose the page')
            page = 1 if page=="" else int(page)
        else:
            return
        self.readbook(title,page)

    def killprocess(self):
        kill = msgbox("Click here to stop reading","Close the Application?")
        if kill is not None:
            os.kill(os.getpid(),signal.SIGTERM)

bookreader = Audiobook()
process1 = Thread(target=bookreader.title)
process2 = Thread(target=bookreader.killprocess)
process2.start()
process1.start()
于 2020-10-25T19:24:22.763 回答