2
3

More than 1 year has passed since last update.

【Python】Tkinterで多言語翻訳アプリを作ってみた(Part 1)

Last updated at Posted at 2022-07-06

はじめに

以前から翻訳アプリを作りたくてウェブで調べていたら、googletransという便利なライブラリーがあったので作成してみました。

環境
・Python3.10 64bit
・Windows11

ライブラリーのインストール

1.speech_recognitionのインストール

%pip install SpeechRecognition

2.googletransのインストール

%pip install googletrans

googletransについては、インストールの過程で苦戦しました。
Youtubeでソリューションがのっていたので、以下の動画を参考にしました。

googletransの言語リスト

googletransの提供している言語リストは、こちらのURLを参考にしてください。

今回使用した言語は以下の2つです。

言語 ISO-639-1コード
日本語 ja
スペイン語 es
import speech_recognition as sr
import googletrans
import gtts
import playsound
import tkinter

Bob = googletrans.Translator()

''' 入力する言語(日本語はja-JP)と出力する言語の指定。'''
input_lang = 'ja-JP'
output_lang = 'es'

'''翻訳ボタンの関数'''
def click_btn():
    text = text_a.get('1.0', 'end - 1c')
    translated = Bob.translate(text, dest = output_lang)
    text_b.delete('1.0', tkinter.END)
    text_b.insert(tkinter.END, translated.text)
    audio = gtts.gTTS(translated.text, lang = output_lang)
    audio.save('romantic.mp3')
    playsound.playsound('romantic.mp3')
'''消去ボタンの関数'''
def del_btn():
    text_b.delete('1.0', 'end -1c')

root = tkinter.Tk()
root.geometry('1000x1000')
root.title('多言語翻訳アプリ')
btn = tkinter.Button(text = 'Translation', font = ('Courier', 30), command = click_btn)
btn.place(x = 180, y = 700)
btn_d = tkinter.Button(text = 'DELETE', font = ('Courier', 30), command = del_btn)
btn_d.place(x = 550, y = 700)
text_a = tkinter.Text(width = 50, height = 10, font = ('Courier', 16), bg = 'lightyellow')
text_a.place(x = 150, y = 6)
text_b = tkinter.Text(width = 50, height = 10, font = ('Courier', 16))
text_b.place(x = 150, y = 400)

root.mainloop()

gif.gif

翻訳した内容しか出てきてませんが、スペイン語も話します。

2
3
0

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
2
3