いろいろ花の写真など撮ってるけどなかなか名前を覚えらないのでので、クイズ形式にでもすれば覚えられるかもしれませんね。
取敢えず何か書き出せば、それらしものが出来るので書き始めてみます。
① 概略は四択Q&A方式 手順
・ウインドーにQ欄とA欄を作る
・花の写真を1枚表示し、4択の回答を表示する
・選んだ回答の正誤に応じた表示をする
・「Nextボタン」で次の問題を出す
・「終わり」で終了
② フォ-ムを作る 現時点での課題
・importするライブラリーが画像はpng限定でjpgは読めません。
勿論どちらでも読めるライブラリーもありますが、自分はPILが使いやすいので今回はそれで行きます。
・画像の大きさに合わせて画像用ラベルは伸縮するようですが、画像をある程度の大きさに揃えておく必要がありそうです。
・ラジオボタンは動的貼付けが簡単そうですが、これもまず一個ずつ張り付けていこうと思います。
・誤回答も含めて4個の変数に花の名前を、どのように格納するか?
・どのラジオボタンが選ばれたのgetと、正解と比較をどのようにするか?
・当たりと外れの音声(ピンポーン、ブー)を発生させるか?
・正解表示用のボタンを追加した方がよいかどうか?
等を検討してプログラムを書いていきます。
③ 四択問題の基本的なフォームは下記
1 import tkinter as tk | ||
2 import random | ||
3 from PIL import Image, ImageTk | ||
4 | ||
5 root = tk.Tk() | ||
6 root.title(“花の名前クイズ”) | ||
7 root.geometry(“400×400”) | ||
8 | ||
9 label1=tk.Label(root,text=’この花の名前はどれでしょう?’) | ||
10 label1.place(x=5, y=10) | ||
11 | ||
12 #Command Button1 | ||
13 btn1=tk.Button(root, text=’次へ/マウスoff時解答’)#commandはこれから | ||
14 btn1.place(x=75, y=350) | ||
15 | ||
16 # 画像の取得 | ||
17 img = tk.PhotoImage(file=’カンパニューラ.png’) | ||
18 | ||
19 # 画像ウィジェットの配置(1行1列) | ||
20 label2 = tk.Label(root, image=img) | ||
21 label2.place(x=25, y=35) | ||
22 | ||
23 label3=tk.Label(root, text=’ 答えは カンパニューラ です。’) | ||
24 label3.place(x=20, y=320) | ||
25 | ||
26 #ラジオボタンを4個作る | ||
27 flw1=’カンパニューラ’ | ||
28 radio1=tk.Radiobutton(root, text=flw1) | ||
29 radio1.place(x=20,y=220) | ||
30 | ||
31 flw2=’朝顔’ | ||
32 radio2=tk.Radiobutton(root, text=flw2) | ||
33 radio2.place(x=20,y=240) | ||
34 | ||
35 flw3=’ベゴニア’ | ||
36 radio3=tk.Radiobutton(root, text=flw3) | ||
37 radio3.place(x=20,y=260) | ||
38 | ||
39 flw4=’ほととぎす’ | ||
40 radio4=tk.Radiobutton(root, text=flw4) | ||
41 radio4.place(x=20,y=280) | ||
42 | ||
43 def quit(): | ||
44 root.destroy() | ||
45 | ||
46 btn2=tk.Button(root, text=’終わる’, command=quit) | ||
47 btn2.place(x=250, y=350) | ||
48 | ||
49 root.mainloop() |
ここまでのプログラムで次のようなformまで出来上がりました。
あとは乱数で画像・4択回答・正解などの表示を出来るようにすることです。
ここからが苦労しそうです。
④ 花の辞書から4個乱数で選ぶ
flowers = {“クンシラン.png”: “”, #4個の花の名前を取り出して変数に格納したい
|
これをrunすると
クレオメ.png |
とprintしてみると上手く行ってそうですが、この乱数で選択した花の名を変数に格納しなければいけません。
それがうまく行かないのです。乱数選択を4回繰り返すと、変数には最後の選択文が入ってしまいます。
と云うわけでこのやり方は失敗でした。 また考えます((´;ω;`)。
⑤ 結論から書きますと何とか出来ました。
いままで試してきたやり方は、可なり上級者のやり方だと思います。 例えばrunするとすぐ関数が処理をはじめて、また別の関数を呼びに行くというような方法でした。 この方法は複雑すぎて自分には完遂出来ませんでした。 大袈裟に言うとアルゴリズムが理解できていなかったということでした。
そこで、code by codeで簡単明瞭に一つ一つ進んでいく方を取りました。まずは全体のプログラムを書きます。
格ステップのcodeの意味は別稿で説明していきます。 2週間もかかってしまった(Sigh!)。
1 | # ボタンに花の名前4.py 5/26/2020 |
2 | # boot時とnextで処理を分けた。 |
3 | # 正解時に「名前が表示される、ピンポーンと影で鳴る」を付けた。 |
4 | |
5 | import tkinter as tk |
6 | import random |
7 | from PIL import Image, ImageTk |
8 | |
9 | root = tk.Tk() |
10 | root.geometry(‘400×420’) |
11 | root.title(‘花の名前当てクイズ’) |
12 | |
13 | label1 = tk.Label(root, text = “この花の名前を当てましょう。”) |
14 | label1.place(x=5, y=5) |
15 | |
16 | label3 = tk.Label(root, text=”正解なら、ここに花の名前が出ます: ”) |
17 | label3.place(x=5, y=320) |
18 | |
19 | text1 = tk.Entry(root, text=”ここに正解が出ます。”) |
20 | text1.place(x=10, y=350) |
21 | |
22 | #今はjpg画像は10枚だけ(このプログラムと同じholderに置くこと) |
23 | flowers = [‘ウキツリボク.jpg’, |
24 | ‘オンジューム.jpg’, |
25 | ‘カタバミ.jpg’, |
26 | ‘カンパニューラ.jpg’, |
27 | ‘キンシバイ.jpg’, |
28 | ‘クレオメ.jpg’, |
29 | ‘クンシラン.jpg’, |
30 | ‘ジキタリス.jpg’, |
31 | ‘シューカイドウ.jpg’, |
32 | ‘ムスカリ.jpg’, |
33 | ] |
34 | #run時に毎回表示する画像を変える |
35 | flower = random.sample(flowers, 1) |
36 | img= Image.open(flower[0]) |
37 | img = ImageTk.PhotoImage(img) |
38 | |
39 | label2 = tk.Label(root,image = img) |
40 | label2.pack(side = “bottom”, fill = “both”, expand = “yes”) |
41 | label2.place(x=10,y=30) |
42 | |
43 | #### ここまではrun時にjpgを表示する OK |
44 | |
45 | # NEXTボタンをクリックした時に写真をラベルに張り付ける動作 |
46 | def push_next_button(): |
47 | text1.delete(0, tk.END) #花の名前の初期化 |
48 | |
49 | photo_file = random.sample(flowers, 4) |
50 | photo_file_0 = photo_file[0] |
51 | photo_file_1 = photo_file[1] |
52 | photo_file_2 = photo_file[2] |
53 | photo_file_3 = photo_file[3] |
54 | |
55 | names = [photo_file_0, photo_file_1, photo_file_2, photo_file_3] |
56 | name = random.sample(names,1) |
57 | #print(name) #[‘カタバミ.jpg’] |
58 | name = name[0] |
59 | #print(“正解は: ”,name) |
60 | |
61 | img_next = Image.open(name) |
62 | img_next = ImageTk.PhotoImage(img_next) |
63 | label2.configure(image = img_next) |
64 | label2.photo = img_next |
65 | |
66 | def get_花名0(): |
67 | print(photo_file_0) |
68 | if photo_file_0 == name: |
69 | #print(“ピンポーン!”) |
70 | text1.insert(tk.END,photo_file_0[:-4]) |
71 | else: |
72 | #print(“ブー!”) |
73 | text1.delete(0, tk.END) |
74 | |
75 | def get_花名1(): |
76 | print(photo_file_1) |
77 | if photo_file_1 == name: |
78 | #print(“ピンポーン!”) |
79 | text1.insert(tk.END,photo_file_1[:-4]) |
80 | else: |
81 | #print(“ブー!”) |
82 | text1.delete(0, tk.END) |
83 | |
84 | def get_花名2(): |
85 | print(photo_file_2) |
86 | if photo_file_2 == name: |
87 | #print(“ピンポーン!”) |
88 | text1.insert(tk.END,photo_file_2[:-4]) |
89 | else: |
90 | #print(“ブー!”) |
91 | text1.delete(0, tk.END) |
92 | |
93 | def get_花名3(): |
94 | print(photo_file_3) |
95 | if photo_file_3 == name: |
96 | #print(“ピンポーン!”) |
97 | text1.insert(tk.END,photo_file_3[:-4]) |
98 | else: |
99 | #print(“ブー!”) |
100 | text1.delete(0, tk.END) |
101 | |
102 | button0 =tk.Button(root, width = 20, text = photo_file_0[:-4], command = get_花名0) |
103 | button0.place(x=230, y=80) |
104 | |
105 | button1 =tk.Button(root, width = 20, text = photo_file_1[:-4], command = get_花名1) |
106 | button1.place(x=230, y=110) |
107 | |
108 | button2 =tk.Button(root, width = 20, text = photo_file_2[:-4], command = get_花名2) |
109 | button2.place(x=230, y=140) |
110 | |
111 | button3 =tk.Button(root, width = 20, text = photo_file_3[:-4], command = get_花名3) |
112 | button3.place(x=230, y=170) |
113 | |
114 | btn1=tk.Button(root, text=’開始/次へ’, command=push_next_button) |
115 | btn1.place(x=180 ,y=350) |
116 | |
117 | #ボタン「終わる」が押されたときの処理 |
118 | def destroy(): |
119 | root.destroy() |
120 | |
121 | btn4 = tk.Button(root, text=’終わる’, command = destroy) |
122 | btn4.place(x=280, y=350) |
123 | |
124 | root.mainloop() |
これで無事花の写真の切替えと4個の選択肢が表示されます。
正しい名前を選ぶと、その名前が表示され間違うとなにも
変化が有りません。 勿論正解の花の名前は毎回表示される位置は
異なります。— その辺苦労しました。
この後必要なら音声も入れられますが、今はスクリプトで
「ピンポーン」とか「ブー」とか言ってる程度です。
まぁ、Python学習とお遊びを兼ねたアプリですからこの辺でいいでしょう。