How to pause, resume and stop pyttsx3 from speaking?

Lenovo 360

Is there any way to pause , resume and stop pyttsx3 from speaking?

I've tried many ways but I couldn't find a solution.

Here's the code:

from tkinter import *
import pyttsx3
import threading

root = Tk()

def read():
    engine.say(text.get(1.0 , END))
    engine.runAndWait()

def stop():
    # Code to stop pyttsx3 from speaking
    pass

def pause():
    # Code to pause pyttsx3
    pass

def unpause():
    # Code to unpause pyttsx3
    pass

engine = pyttsx3.init()

text = Text(width = 65 , height = 20 , font = "consolas 14")
text.pack()

text.insert(END , "This is a text widget\n"*10)

read_button = Button(root , text = "Read aloud" , command = lambda: threading.Thread(target=read, daemon=True).start())
read_button.pack(pady = 20)

pause_button = Button(root , text = "Pause" , command = lambda: threading.Thread(target=pause , daemon = True).start())
pause_button.pack()

unpause_button = Button(root , text = "Unpause" , command = unpause)
unpause_button.pack(pady = 20)

stop_button = Button(root , text = "Stop" , command = threading.Thread(target=stop, daemon = True).start())
stop_button.pack()

mainloop()

What I want is to pause, resume, and stop pyttsx3 whenever I want by clicking the buttons.

Is there any way to achieve this in tkinter?

It would be great if anyone could help me out.

acw1668

There is no pause and resume function provided by pyttsx3, but you can simulate these feature by splitting the sentence into words, and then speak word by word. In between words, you can check whether pause, unpause or stop button is clicked and do corresponding action:

from tkinter import *
import pyttsx3
import threading

root = Tk()

class Speaking(threading.Thread):
    def __init__(self, sentence, **kw):
        super().__init__(**kw)
        self.words = sentence.split()
        self.paused = False

    def run(self):
        self.running = True
        while self.words and self.running:
            if not self.paused:
                word = self.words.pop(0)
                print(word)
                engine.say(word)
                engine.runAndWait()
        print("finished")
        self.running = False

    def stop(self):
        self.running = False

    def pause(self):
        self.paused = True

    def resume(self):
        self.paused = False

speak = None

def read():
    global speak
    if speak is None or not speak.running:
        speak = Speaking(text.get(1.0, END), daemon=True)
        speak.start()

def stop():
    global speak
    if speak:
        speak.stop()
        speak = None

def pause():
    if speak:
        speak.pause()

def unpause():
    if speak:
        speak.resume()

engine = pyttsx3.init()

text = Text(width=65, height=20, font="consolas 14")
text.pack()

text.insert(END, "This is a text widget\n"*10)

read_button = Button(root, text="Read aloud", command=read)
read_button.pack(pady=20)

pause_button = Button(root, text="Pause", command=pause)
pause_button.pack()

unpause_button = Button(root, text="Unpause", command=unpause)
unpause_button.pack(pady=20)

stop_button = Button(root, text="Stop", command=stop)
stop_button.pack()

mainloop()

Update: Using pygame as the player:

from tkinter import *
import pyttsx3
import pygame

pygame.mixer.init()
engine = pyttsx3.init()

root = Tk()

def read():
    outfile = "temp.wav"
    engine.save_to_file(text.get('1.0', END), outfile)
    engine.runAndWait()
    pygame.mixer.music.load(outfile)
    pygame.mixer.music.play()

def stop():
    pygame.mixer.music.stop()

def pause():
    pygame.mixer.music.pause()

def unpause():
    pygame.mixer.music.unpause()


text = Text(width=65, height=20, font="consolas 14")
text.pack()

text.insert(END, "This is a text widget\n"*10)

read_button = Button(root, text="Read aloud", command=read)
read_button.pack(pady=20)

pause_button = Button(root, text="Pause", command=pause)
pause_button.pack()

unpause_button = Button(root, text="Unpause", command=unpause)
unpause_button.pack(pady=20)

stop_button = Button(root, text="Stop", command=stop)
stop_button.pack()

mainloop()

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Pause Phone Ringtone while Speaking through Text To Speech and then Resume

분류에서Dev

Playing music in PlayN: how to pause, resume?

분류에서Dev

How to start/resume an Oracle VirtualBox and immediately after pause it?

분류에서Dev

ConnectionRequest pause () 및 resume ()

분류에서Dev

SpriteKit Pause Resume 오류

분류에서Dev

Rhino: Ability to pause, save state and resume javascript

분류에서Dev

Pause wget and resume when closing terminal

분류에서Dev

How to set Windows not to prompt for password on resume from sleep

분류에서Dev

How to stop user from login?

분류에서Dev

What's the difference in using Pause/Stop in µTorrent?

분류에서Dev

How to stop a remote desktop session from ending

분류에서Dev

How to stop table from adjusting width

분류에서Dev

Pause timer if user presses up or down key, resume when they don't

분류에서Dev

Resume from suspend - all programs are closed 12.04

분류에서Dev

Ubuntu 15.10: Cannot resume from hibernate

분류에서Dev

Extracting multiple fields from resume with Python

분류에서Dev

Xubuntu 14.04 fails to resume from hibernation

분류에서Dev

How to pause and unpause thread in a game?

분류에서Dev

How can I stop gvim from crashing when opening a file?

분류에서Dev

How to stop USB full ubuntu install from probing hard disks?

분류에서Dev

How can I stop terminal and firefox from launching on startup?

분류에서Dev

How can I stop windows from always booting first?

분류에서Dev

How do I stop gedit from opening anything?

분류에서Dev

How can I stop pages from scrolling with an empty hyperlink jump?

분류에서Dev

How to stop MAC address from changing after disconnecting?

분류에서Dev

How do I stop Thunderbird from forgetting my passwords?

분류에서Dev

How can I stop excel from rounding to .00?

분류에서Dev

How to stop Ubuntu from changing numlock state on boot?

분류에서Dev

How to call computation.stop() from within Deps.autorun()?

Related 관련 기사

  1. 1

    Pause Phone Ringtone while Speaking through Text To Speech and then Resume

  2. 2

    Playing music in PlayN: how to pause, resume?

  3. 3

    How to start/resume an Oracle VirtualBox and immediately after pause it?

  4. 4

    ConnectionRequest pause () 및 resume ()

  5. 5

    SpriteKit Pause Resume 오류

  6. 6

    Rhino: Ability to pause, save state and resume javascript

  7. 7

    Pause wget and resume when closing terminal

  8. 8

    How to set Windows not to prompt for password on resume from sleep

  9. 9

    How to stop user from login?

  10. 10

    What's the difference in using Pause/Stop in µTorrent?

  11. 11

    How to stop a remote desktop session from ending

  12. 12

    How to stop table from adjusting width

  13. 13

    Pause timer if user presses up or down key, resume when they don't

  14. 14

    Resume from suspend - all programs are closed 12.04

  15. 15

    Ubuntu 15.10: Cannot resume from hibernate

  16. 16

    Extracting multiple fields from resume with Python

  17. 17

    Xubuntu 14.04 fails to resume from hibernation

  18. 18

    How to pause and unpause thread in a game?

  19. 19

    How can I stop gvim from crashing when opening a file?

  20. 20

    How to stop USB full ubuntu install from probing hard disks?

  21. 21

    How can I stop terminal and firefox from launching on startup?

  22. 22

    How can I stop windows from always booting first?

  23. 23

    How do I stop gedit from opening anything?

  24. 24

    How can I stop pages from scrolling with an empty hyperlink jump?

  25. 25

    How to stop MAC address from changing after disconnecting?

  26. 26

    How do I stop Thunderbird from forgetting my passwords?

  27. 27

    How can I stop excel from rounding to .00?

  28. 28

    How to stop Ubuntu from changing numlock state on boot?

  29. 29

    How to call computation.stop() from within Deps.autorun()?

뜨겁다태그

보관