【改訂】計算問題を解いてダイエット(pywin32を使って問題を読んでもらう)

はじめに

以前にこのような記事を書きました。
touch-sp.hatenablog.com
ただひたすら掛け算問題を読み上げてくれるプログラムでした。

以前は音声ファイルをダウンロードしてそれを「python-vlc」を使って再生していました。

今回「pywin32」を使ってプログラムを書き換えました。

「pywin32」を使用すると音声ファイルのダウンロードやVLC Playerのインストールが不要になります。

Pythonスクリプト

import win32com.client
import random

speaker = win32com.client.Dispatch('SAPI.SpVoice')

while True:
    q1 = random.randint(10,99)
    q2 = random.randint(10,99)

    answer = q1 * q2

    sound_q = '%dかける%d'%(q1, q2)
    sound_a = '%d'%answer

    speaker.Speak(sound_q)

    get_key = input()
    if get_key == 'q':
        break

    speaker.Speak(sound_a)

    get_key = input()
    if get_key == 'q':
        break

ずいぶんすっきりしました。

環境

Windows 11
python 3.9.11
pywin32==303

未解決問題

「110」を「ひゃくとうばん」と読み上げます(笑)。

2022年3月21日追記(PyQt6でGUIを作りました)

import sys
import win32com.client
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QFont
import random

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()
        self.speaker = win32com.client.Dispatch('SAPI.SpVoice')
        self.q1 = 0
        self.q2 = 0
        self.answer = 0
        self.playing = False
        
    def initUI(self):
        self.setWindowTitle("かけ算")

        font = QFont()
        font.setFamily('Times')
        font.setPointSize(20)
        font.setBold(True)

        self.num_label = QLabel()
        self.num_label.setFixedSize(QSize(300,100))
        self.num_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.setFont(font)
        self.num_label.setText('start')

        layout = QVBoxLayout()
        layout.addWidget(self.num_label)
        self.setLayout(layout)

    def keyPressEvent(self, e):

        if e.key() == Qt.Key.Key_N:
            self.calc_exe()
        
        if e.key() == Qt.Key.Key_Q:
            sys.exit()

    def calc_exe(self):
        if self.playing == False:
            self.q1 = random.randint(10,19)
            self.q2 = random.randint(10,19)
            self.num_label.setText('%d x %d'%(self.q1, self.q2))
            self.answer = self.q1 * self.q2
            self.speaker.Speak('%dかける%d'%(self.q1, self.q2))
            self.playing = not self.playing
        else:
            self.speaker.Speak('%d'%self.answer)
            self.playing = not self.playing

if __name__ == "__main__":
    app = QApplication([])
    ex =Window()
    ex.show()
    app.exec()

2022年3月23日追記(スクリプトの改訂)

「constructGUI.py」を使ってPythonスクリプトを書き換えました。

「constructGUI.py」に関してはこちらを参照して下さい。
touch-sp.hatenablog.com

Pythonスクリプト

import sys
import random
import win32com.client
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel

from constructGUI import construct

class Window(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()
        self.speaker = win32com.client.Dispatch('SAPI.SpVoice')
        self.q1 = 0
        self.q2 = 0
        self.answer = 0
        self.playing = False
        
    def initUI(self):
        self.setWindowTitle("かけ算")

        self.num_label = construct(QLabel(), "settings.yaml", "label_1")

        self.setCentralWidget(self.num_label)

    def keyPressEvent(self, e):

        if e.key() == Qt.Key.Key_N:
            self.calc_exe()
        
        if e.key() == Qt.Key.Key_Q:
            sys.exit()

    def calc_exe(self):
        if self.playing == False:
            self.q1 = random.randint(10,25)
            self.q2 = random.randint(10,25)
            self.num_label.setText('%d x %d'%(self.q1, self.q2))
            self.answer = self.q1 * self.q2
            self.speaker.Speak('%dかける%d'%(self.q1, self.q2))
            self.playing = not self.playing
        else:
            self.speaker.Speak('%d'%self.answer)
            self.playing = not self.playing

if __name__ == "__main__":
    app = QApplication([])
    ex =Window()
    ex.show()
    app.exec()

YAMLファイル

label_1:
  type: QLabel
  width: 300
  height: 100
  alignment: center
  fontFamily: times
  fontPoint: 20
  fontBold: True
  text: start