質問編集履歴

1

とりあえず、この方法で希望する動作が出来るようになりましたが、もっと良い方法があれば教えてください。宜しくお願いします。

2018/06/11 04:35

投稿

yoko4401
yoko4401

スコア9

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,117 @@
1
- ちは。Python初心者です。tkinterでmainloopにる前にkey=msvcrt.getwch()でキー入された値を
1
+ ```言語を入力
2
2
 
3
- keyに代入したいのですが、すぐにmainloop()に入ってしまい、keyを取り出すことができません。どのようにしたら
3
+ import tkinter
4
4
 
5
+ import time
6
+
7
+ import msvcrt
8
+
9
+ import keyboard
10
+
11
+
12
+
13
+ questions1=[
14
+
15
+ {"question":"問1","answer":"B"},
16
+
17
+ {"question":"問2","answer":"A"},
18
+
19
+ {"question":"問3","answer":"B"},
20
+
21
+ {"question":"問4","answer":"A"},
22
+
23
+ {"question":"問5","answer":"B"},
24
+
25
+ ]
26
+
27
+
28
+
29
+ pimg = tkinter.PhotoImage(file="img/Q1.png")
30
+
31
+ trueimg = tkinter.PhotoImage(file="img/A1.png")
32
+
33
+ falseimg = tkinter.PhotoImage(file="img/A2.png")
34
+
35
+
36
+
37
+ root = tkinter.Tk()
38
+
39
+ root.minsize(320, 240)
40
+
41
+ canvas = tkinter.Canvas(root, width=320, height=240)
42
+
43
+ canvas.place(x=0, y=0)
44
+
45
+ canvas.create_image(320, 240, image=pimg)
46
+
47
+ root.update()
48
+
49
+
50
+
51
+ index=random.randint(5)
52
+
53
+
54
+
55
+ ans=questions1[index]['answer']
56
+
57
+
58
+
59
+ while True:
60
+
61
+ try:
62
+
5
- keyを取り出せますか。他に簡単な方法があればそちらでも結構です。
63
+ if keyboard.is_pressed('a'):
64
+
65
+ print('You Pressed a Key!') # 動作確認のため
66
+
67
+ keyP="A"
68
+
69
+ break
70
+
71
+ elif keyboard.is_pressed('b'):
72
+
73
+ print('You Pressed b Key!') # 動作確認のため
74
+
75
+ keyP="B"
76
+
77
+ break
78
+
79
+ else:
80
+
81
+ pass
82
+
83
+ except:
84
+
85
+ break
86
+
87
+ print(keyP) # 動作確認のため
88
+
89
+
90
+
91
+
92
+
93
+ if keyP==ans :
94
+
95
+ canvas.place(x=0, y=0)
96
+
97
+ canvas.create_image(320, 220, image=trueimg,tag="illust")
98
+
99
+ root.update()
100
+
101
+ time.sleep(1)
102
+
103
+ else:
104
+
105
+ canvas.place(x=0, y=0)
106
+
107
+ canvas.create_image(320, 220, image=falseimg,tag="illust")
108
+
109
+ root.update()
110
+
111
+ time.sleep(1)
112
+
113
+
114
+
115
+ root.mainloop()
116
+
117
+ ```