① 歌の表示は完成
前回までで取り合えずGUIで乱数に応じた百人一首が表示されるようになりました。
更に進化するために、次のようなことを考えております。
・上の句・下の句・歌人・現代訳を乱数に合わせて表示する <– 前回までで完成済み
・歌に合わせて百人一首の絵(JPG)を表示する
・上の句に応じた下の句の正解を4-5選択肢から選ぶ
・当たれば「ピンポーン」、外れれば「ブー」の音を出す
・また当落に応じたコメントを出す(文字)
・歌の読み上げ・BGMの有り無しを選択できるようにする
・アプリを止めるときに、正解・不正解の成績を表示する
途中までのGUIを上げておきます。
② ここまでのcode(まだjpgは切り替わらない)
ご注意:真似する人はいないと思うけれど、
Pythonはcodeのindentは変数の有効範囲に
影響しますので確認ください。
#百人一首2.py 5/201/2019
#jpgにはPILが必要
#PhotoImagePPM、GIFが表示できる
import tkinter as tk
import random
from time import sleep
import sys
from PIL import Image, ImageTk
import urllib.request as req
root = tk.Tk()
root.geometry(‘500×400’)
root.title(‘クマさんの百人一首’)
canvas = tk.Canvas(
root, # 親要素をメインウィンドウに設定
width=500, # 幅を設定
height=500, # 高さを設定
relief=tk.RIDGE # 枠線を表示
# 枠線の幅の設定はまだしていない
)
canvas.place(x=0, y=0) # メインウィンドウ上に配置
#PILでjpgを使用
img1 = Image.open(open(’10.jpg’, ‘rb’))
img1.thumbnail((200, 300), Image.ANTIALIAS)
img1 = ImageTk.PhotoImage(img1) # 表示するイメージを用意
canvas.create_image( # キャンバス上にイメージを配置
10, # x座標
10, # y座標
image=img1, # 配置するイメージオブジェクトを指定
tag=”illust”, # タグで引数を追加する。
anchor=tk.NW # 配置の起点となる位置を左上隅に指定
)
# Label
lbl1 = tk.Label(text=’上の句:’)
lbl1.place(x=220, y=30)
lbl2 = tk.Label(text=’下の句:’)
lbl2.place(x=220, y=80)
lbl3 = tk.Label(text=’歌人:’)
lbl3.place(x=220, y=140)
lbl4 = tk.Label(text=’現代訳:’)
lbl4.place(x=220, y=200)
# Text(Entry)
txt1 = tk.Entry(width=40)
txt1.place(x=225, y=50)
txt2 = tk.Entry(width=40)
txt2.place(x=225, y=100)
txt3 = tk.Entry(width=15)
txt3.place(x=225, y=160)
txt4 = tk.Entry(width=40) #複数行入力故entryはNGでは?
#txt4.place(x=225, y=220) #使用はするが即データをmessageboxへ渡す。 Entryの表示はしない
#Command Button1
btn1=tk.Button(root, text=’開始/次へ’)
btn1.place(x=100, y=330)
def read_words():
file = open(‘百人一首10.txt’,’r’, encoding=’utf_8′)
lines = file.readlines()
file.close()
kami_words = [] # 上の句list
shimo_words = [] # 下の句list
poet_words = [] # 歌人list
trans_words = [] # 現代訳list
line_count = 0 #句数カウンター
for line in lines:
# line = line.rstrip(“\n”) #有ってもなくても変化いみたい
name = line.rsplit(“\t”)
if len(name) == 4: # 要素4個をそれぞれのlistへ追加する
kami_words.append(name[0]) # 下記の句listへadd
shimo_words.append(name[1]) # 下の句をlistへadd
poet_words.append(name[2]) # 歌人をlistへadd
trans_words.append(name[3]) # 現代訳をlistへadd
line_count += 1
return line_count,[kami_words, shimo_words, poet_words, trans_words] # txtがなくなるまで繰り返す
def show_data(count,num, kami_words, shimo_words, poset_words, trans_words): # 乱数に応じた要素を表示する関数
kami_word = kami_words[num] # 俳句をh_wordに格納
shimo_word = shimo_words[num]
poet_word = poet_words[num]
trans_word = trans_words[num]
#ボタン1(俳句作る)の関数 OK
def issyuShow(event):
num = random.randrange(0, count)
#print(f’num={num}’) #変数numの表示
kami_word = kami_words[num] # 俳句をh_wordに格納
shimo_word = shimo_words[num]
poet_word = poet_words[num]
trans_word = trans_words[num]
txt1.delete(0, tk.END)
txt2.delete(0, tk.END)
txt3.delete(0, tk.END)
txt4.delete(0, tk.END)
txt1.insert(0, f’ {kami_word}’) #txt1.insert(tk.END, kami_word)でもOK
txt2.insert(0, f’ {shimo_word}’)
txt3.insert(0, f’ {poet_word}’)
txt4.insert(0, f’ {trans_word}’) #1行のみなので途中で表示が切れる
txt5=txt4.get()
txt6= tk.Message(root,
width=230,
text=txt5, bd=0,relief=”ridge”, bg=”white”
)
txt6.place(x=225, y=220)
btn1.bind(“<Button>”, issyuShow)
count, words = read_words()
kami_words = words[0]
shimo_words = words[1]
poet_words = words[2]
trans_words = words[3]
num = random.randrange(0, count)
show_data(count, num, kami_words, shimo_words, poet_words, trans_words)
def destroy():
global root
root.destroy()
#Command Button2
btn2=tk.Button(root, text=’終わる’, command=destroy)
btn2.place(x=330, y=330)
root.mainloop()