1
0

More than 1 year has passed since last update.

「答えてナナちゃん」アプリケーション

Last updated at Posted at 2022-10-27

1 はじめに

今回は、バーチャルフレンドをテーマにtkinterでアプリケーションを作成しました。
作成の際には以下のポイントに気をつけました。

  1. 与えられた内容に対して、声に出しているか

  2. 入力した内容によって画像が変わっているか?
    (デフォルト時の画像が残らず、新しい画像に切り替わっているか。)

2 まず画像を用意する

使用する画像はデフォルトを含めて、3枚使用しました。
今回使用した画像は「イラストAC」さんから、
使用する表情を一つ一つ切り抜いて作成しました。

3 アニメのボイスを用意する

アニメのボイスにつきましては、「効果音ラボ」から使用しました。

Tkinterで効果音を使用するため、外部ライブラリーのplaysoundをインストールします。

pip install playsound

そのあと、playsoundモジュールを呼び出します。

from playsound import playsound
main.py
import tkinter
from playsound import playsound #効果音ファイルを使用するためplaysoundモジュールをインポートする

root = tkinter.Tk()
root.title('答えててナナちゃん')
root.resizable(False, False)
canvas = tkinter.Canvas(root, width = 900, height = 800, bg = 'white')
canvas.pack()
#画像リストを用意する
list = [
    tkinter.PhotoImage(file = '23110713.png'), 
    tkinter.PhotoImage(file = '23110713j.png'),
    tkinter.PhotoImage(file = '23110713k.png')
]

canvas.create_image(200, 300, image = list[0], tag = 'PH')


def click_btn(): #「ボタン」を押したときの動作
    txt = sub_text.get('1.0', 'end-1c')
    if txt in['おはよう', '頑張って']:
        text.insert(tkinter.END, 'おはよう!とっても嬉しいです')
        playsound('sound_3.mp3')
        playsound('sound_2.mp3')
        canvas.delete('PH')
        canvas.update()
        canvas.create_image(200, 300, image = list[2], tag = 'XH')
        
    if txt in ['かわいくない', 'にきびあるよ']:
        text.insert(tkinter.END, '怒るわよ')
        playsound('sound_1.mp3')
        canvas.delete('PH')
        canvas.update()
        canvas.create_image(200, 300, image = list[1], tag = 'FH')
        
   


def reset_btn(): #「reset」ボタンを押したときの動作
    text.delete('1.0', tkinter.END)
    canvas.delete('XH')
    canvas.delete('FH')
    canvas.create_image(200, 300, image = list[0], tag = 'PH')
    
text = tkinter.Text(width = 40, height = 10, font = ('Lucida Console', 15), relief = 'solid', bg = 'lightblue')
text.place(x = 450, y = 80)
sub_text = tkinter.Text(width = 40, height = 10, font = ('Lucida Console', 15), relief = 'solid', bg = 'lightgreen')
sub_text.place(x = 450, y = 400)
btn = tkinter.Button(text = 'ボタン', font = ('Lucida Console', 25),command = click_btn, bg = 'lightblue')
btn.place(x = 500, y = 650)
r_btn = tkinter.Button(text = 'RESET', font = ('Lucida Console', 25), command = reset_btn, bg = 'lightblue')
r_btn.place(x = 700, y = 650)



root.mainloop()


4 実装

「おはよう」又は「頑張って」を入力すると、「おはよう!とっても嬉しいです」と答えてくれます。

gif (1).gif

「かわいくない」又は「にきびあるよ」を入力すると、「怒るわよ」と答えます。

gif.gif

5 技術的な問題点

  1. 返ってくる答えと音声が同時進行していない。内容と言葉が一致するよう構文を書き換えましたが、バグが起きる。

  2. ボタンを押したらリセットボタンを押さないと、画像が重なり見ずらくなるので、必ずリセットボタを押してから、次の処理に移る。自動でリセットして、処理を進めるにはwhile〜 root.afterを書けばいいのかな?

  3. 実際には、与えられた内容に対して声に出して読むと機械語になり聞き辛いので、「効果音ラボ」にあるボイスと同じ内容のものを流すことにしました。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0