英単語学習アプリをpython3.9.6で作成しているのですが、keycodeを使って入力した値を取得しようとしても、e.keycodeでは小文字のaしか取得することができません。e.keysymは正常に作動しており、returnやbackspace,entryには反応します。どなたか回答していただけるとありがたいです。
python3
1import tkinter 2 3FNT1 = ("Times New Roman", 12) 4FNT2 = ("Times New Roman", 24) 5 6WORDS = [ 7"apple", "リンゴ", 8"book", "本", 9"cat", "猫", 10"dog", "犬", 11"egg", "卵", 12"fire", "火", 13"gold", "金色", 14"head", "頭", 15"ice", "氷", 16"juice", "ジュース", 17"king", "王様", 18"lemon", "レモン", 19"mother", "お母さん", 20"notebook", "ノート", 21"orange", "オレンジ", 22"pen", "ペン", 23"queen", "女王", 24"room", "部屋", 25"sport", "スポーツ", 26"time", "時間", 27"user", "ユーザー", 28"vet", "獣医", 29"window", "窓", 30"xanadu", "桃源郷", 31"yellow", "黄色", 32"zoo", "動物園" 33] 34MAX = int(len(WORDS)/2) 35score = 0 36word_num = 0 37yourword = "" 38koff = False #1文字ずつ入力するためのフラグ 39 40def key_down(e): 41 global score, word_num, yourword, koff 42 if koff == True: 43 koff = False 44 kcode = e.keycode 45 ksym = e.keysym 46 if 65 <= kcode and kcode <= 90: #大文字 47 yourword = yourword + chr(kcode+32) 48 if 97 <= kcode and kcode <= 122: #小文字 49 yourword = yourword + chr(kcode)#ここでaにしか反応しない 50 if ksym == "Delete" or ksym == "BackSpace": 51 yourword = yourword[:-1] # この記述でお尻の1文字を削除 52 input_label["text"] = yourword 53 if ksym == "Return": 54 if input_label["text"] == english_label["text"]: 55 score = score + 1 56 set_label() 57 58def key_up(e): 59 global koff 60 koff = True 61 62def set_label(): 63 global word_num, yourword 64 score_label["text"] = score 65 english_label["text"] = WORDS[word_num*2] 66 japanese_label["text"] = WORDS[word_num*2+1] 67 input_label["text"] = "" 68 word_num = (word_num + 1)%MAX 69 yourword = "" 70 71root = tkinter.Tk() 72root.title("単語学習アプリ") 73root.geometry("400x200") 74root.resizable(False, False) 75root.bind("<KeyPress>", key_down) 76root.bind("<KeyRelease>", key_up) 77root["bg"] = "#DEF" 78 79score_label = tkinter.Label(font=FNT1, bg="#DEF", fg="#4C0") 80score_label.pack() 81english_label = tkinter.Label(font=FNT2, bg="#DEF") 82english_label.pack() 83japanese_label = tkinter.Label(font=FNT1, bg="#DEF", fg="#444") 84japanese_label.pack() 85input_label = tkinter.Label(font=FNT2, bg="#DEF") 86input_label.pack() 87howto_label = tkinter.Label(text="英単語を入力し[Enter]を押す\n入力し直しは[Delete]か[BS]", font=FNT1, bg="#FFF", fg="#ABC") 88howto_label.pack() 89 90set_label() 91root.mainloop()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/18 13:28