回答編集履歴

15

pygame連携について追記

2020/02/16 09:54

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -235,3 +235,133 @@
235
235
  main()
236
236
 
237
237
  ```
238
+
239
+
240
+
241
+ pygameとの連携がうまく動かないということなら、以下のような単純化した処理に置き換えて動作核にしてみてはいかがですか?
242
+
243
+
244
+
245
+ ```py
246
+
247
+ import os
248
+
249
+ import sys
250
+
251
+ import time
252
+
253
+ import threading
254
+
255
+ import pygame
256
+
257
+ from pygame.locals import *
258
+
259
+ os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
260
+
261
+ import tkinter as tk
262
+
263
+ from tkinter import font, IntVar
264
+
265
+
266
+
267
+
268
+
269
+ class Game:
270
+
271
+
272
+
273
+ def __init__(self, title):
274
+
275
+ self.title = title
276
+
277
+
278
+
279
+ def run(self):
280
+
281
+ self.running = True
282
+
283
+ while self.running:
284
+
285
+ time.sleep(1)
286
+
287
+
288
+
289
+ def push_switch(self, number):
290
+
291
+ print("push_switch:", repr(number))
292
+
293
+
294
+
295
+ def quit(self):
296
+
297
+ self.running = False
298
+
299
+
300
+
301
+
302
+
303
+ def main():
304
+
305
+ game = Game('Contoller input')
306
+
307
+ thread = threading.Thread(target=game.run)
308
+
309
+ thread.start()
310
+
311
+ pygame.joystick.init()
312
+
313
+ pygame.init()
314
+
315
+
316
+
317
+ try:
318
+
319
+ j = pygame.joystick.Joystick(0) # create a joystick instance
320
+
321
+ j.init() # init instance
322
+
323
+ print(j.get_name())
324
+
325
+ except pygame.error:
326
+
327
+ print('Joystickが見つかりませんでした。')
328
+
329
+ game.quit()
330
+
331
+
332
+
333
+ for e in pygame.event.get():
334
+
335
+ print(e)
336
+
337
+ if(e.type == pygame.locals.JOYBUTTONDOWN or e.type == pygame.locals.JOYAXISMOTION or e.type == QUIT):
338
+
339
+ if(e.type == QUIT):
340
+
341
+ print("quit")
342
+
343
+ game.quit()
344
+
345
+ thread.join()
346
+
347
+ if(e.type == pygame.locals.JOYBUTTONDOWN):
348
+
349
+ print("button " + str(e.button))
350
+
351
+ if(e.button < 7):
352
+
353
+ game.push_switch(e.button)
354
+
355
+ elif(e.type == pygame.locals.JOYAXISMOTION):
356
+
357
+ print("turntable spin")
358
+
359
+ game.push_switch(7)
360
+
361
+
362
+
363
+ if __name__ == '__main__':
364
+
365
+ main()
366
+
367
+ ```

14

スイッチ番号変更

2020/02/16 09:54

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -110,15 +110,33 @@
110
110
 
111
111
  self.running = True
112
112
 
113
- self.window = window = tk.Tk()
113
+ self.window = tk.Tk()
114
-
114
+
115
- window.title(self.title)
115
+ self.window.title(self.title)
116
-
116
+
117
- window.geometry('640x360')
117
+ self.window.geometry('640x360')
118
+
119
+
120
+
118
-
121
+ self.switches = self._make_switches()
122
+
123
+
124
+
119
-
125
+ self.window.after(1000, self._check_to_quit)
126
+
120
-
127
+ self.window.mainloop()
128
+
129
+ # need to delete variables that reference tkinter objects in the thread
130
+
131
+ del self.switches
132
+
133
+ del self.window
134
+
135
+
136
+
137
+ def _make_switches(self):
138
+
121
- canvas = tk.Canvas(window, width=640, height=360)
139
+ canvas = tk.Canvas(self.window, width=640, height=360)
122
140
 
123
141
  canvas.place(x=0, y=0)
124
142
 
@@ -140,49 +158,39 @@
140
158
 
141
159
  for i, key in enumerate(white_keys):
142
160
 
143
- label = tk.Label(window, font=font1, textvariable=key.count)
161
+ label = tk.Label(self.window, font=font1, textvariable=key.count)
144
162
 
145
163
  label.place(x=310 + 90 * i, y=333)
146
164
 
147
165
  for i, key in enumerate(black_keys):
148
166
 
149
- label = tk.Label(window, font=font1, textvariable=key.count)
167
+ label = tk.Label(self.window, font=font1, textvariable=key.count)
150
168
 
151
169
  label.place(x=355 + 90 * i, y=5)
152
170
 
153
- label = tk.Label(window, font=font1, textvariable=disk.count)
171
+ label = tk.Label(self.window, font=font1, textvariable=disk.count)
154
172
 
155
173
  label.place(x=205, y=300)
156
174
 
157
175
 
158
176
 
159
- self.keys = dict(zip((0, 2, 4, 6, 1, 3, 5, 7),
177
+ return dict(zip((0, 2, 4, 6, 1, 3, 5, 7),
160
-
178
+
161
- (*white_keys, *black_keys, disk)))
179
+ (*white_keys, *black_keys, disk)))
162
-
163
- self.window.after(1000, self.check_to_quit)
164
-
165
- self.window.mainloop()
166
-
167
- # need to delete variables that reference tkinter objects
168
-
169
- del self.keys
170
-
171
- del self.window
172
180
 
173
181
 
174
182
 
175
183
  def push_switch(self, index):
176
184
 
177
- self.keys[index].push()
185
+ self.switches[index].push()
178
-
179
-
180
-
186
+
187
+
188
+
181
- def check_to_quit(self):
189
+ def _check_to_quit(self):
182
190
 
183
191
  if self.running:
184
192
 
185
- self.window.after(1000, self.check_to_quit)
193
+ self.window.after(1000, self._check_to_quit)
186
194
 
187
195
  else:
188
196
 

13

ボタン順序変更

2020/02/12 08:35

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -134,8 +134,6 @@
134
134
 
135
135
  disk = Disk(canvas, x=35, y=65, size=220)
136
136
 
137
- self.keys = [*white_keys, *black_keys, disk]
138
-
139
137
 
140
138
 
141
139
  font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
@@ -158,6 +156,10 @@
158
156
 
159
157
 
160
158
 
159
+ self.keys = dict(zip((0, 2, 4, 6, 1, 3, 5, 7),
160
+
161
+ (*white_keys, *black_keys, disk)))
162
+
161
163
  self.window.after(1000, self.check_to_quit)
162
164
 
163
165
  self.window.mainloop()

12

変数削除が必要なことをコメント追記

2020/02/12 06:02

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -162,6 +162,8 @@
162
162
 
163
163
  self.window.mainloop()
164
164
 
165
+ # need to delete variables that reference tkinter objects
166
+
165
167
  del self.keys
166
168
 
167
169
  del self.window

11

exit()削除

2020/02/12 04:39

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -216,8 +216,6 @@
216
216
 
217
217
  thread.join()
218
218
 
219
- exit()
220
-
221
219
 
222
220
 
223
221
  if __name__ == '__main__':

10

終了処理変更

2020/02/12 04:28

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -162,6 +162,10 @@
162
162
 
163
163
  self.window.mainloop()
164
164
 
165
+ del self.keys
166
+
167
+ del self.window
168
+
165
169
 
166
170
 
167
171
  def push_switch(self, index):
@@ -180,10 +184,6 @@
180
184
 
181
185
  self.window.destroy()
182
186
 
183
- del self.keys
184
-
185
- del self.window
186
-
187
187
 
188
188
 
189
189
  def quit(self):
@@ -198,8 +198,6 @@
198
198
 
199
199
  thread = threading.Thread(target=game.run)
200
200
 
201
- thread.setDaemon(True)
202
-
203
201
  thread.start()
204
202
 
205
203
 

9

終了処理変更

2020/02/12 04:25

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -178,11 +178,11 @@
178
178
 
179
179
  else:
180
180
 
181
- self.keys = None
182
-
183
181
  self.window.destroy()
184
182
 
183
+ del self.keys
184
+
185
- self.window = None
185
+ del self.window
186
186
 
187
187
 
188
188
 

8

正常終了するように修正

2020/02/12 03:30

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  tkinterだけの処理を書いてみました。
6
6
 
7
- 残念ながら、スレッド終了がうまくいかなくてコマンド終了きませんけど、動作確認はできます
7
+ このプログラムだけで動作確認してみてください
8
8
 
9
9
 
10
10
 
@@ -162,8 +162,6 @@
162
162
 
163
163
  self.window.mainloop()
164
164
 
165
- print("exit mainloop()")
166
-
167
165
 
168
166
 
169
167
  def push_switch(self, index):
@@ -184,6 +182,8 @@
184
182
 
185
183
  self.window.destroy()
186
184
 
185
+ self.window = None
186
+
187
187
 
188
188
 
189
189
  def quit(self):

7

quit処理変更

2020/02/12 03:26

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -108,6 +108,8 @@
108
108
 
109
109
  def run(self):
110
110
 
111
+ self.running = True
112
+
111
113
  self.window = window = tk.Tk()
112
114
 
113
115
  window.title(self.title)
@@ -156,8 +158,12 @@
156
158
 
157
159
 
158
160
 
161
+ self.window.after(1000, self.check_to_quit)
162
+
159
163
  self.window.mainloop()
160
164
 
165
+ print("exit mainloop()")
166
+
161
167
 
162
168
 
163
169
  def push_switch(self, index):
@@ -166,11 +172,23 @@
166
172
 
167
173
 
168
174
 
175
+ def check_to_quit(self):
176
+
177
+ if self.running:
178
+
179
+ self.window.after(1000, self.check_to_quit)
180
+
181
+ else:
182
+
183
+ self.keys = None
184
+
185
+ self.window.destroy()
186
+
187
+
188
+
169
189
  def quit(self):
170
190
 
171
- self.window.destroy()
191
+ self.running = False
172
-
173
-
174
192
 
175
193
 
176
194
 
@@ -180,6 +198,8 @@
180
198
 
181
199
  thread = threading.Thread(target=game.run)
182
200
 
201
+ thread.setDaemon(True)
202
+
183
203
  thread.start()
184
204
 
185
205
 

6

メソッド名変更

2020/02/12 03:23

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- def count_up(self):
47
+ def push(self):
48
48
 
49
49
  self.count.set(self.count.get() + 1)
50
50
 
@@ -160,9 +160,9 @@
160
160
 
161
161
 
162
162
 
163
- def count_up(self, index):
163
+ def push_switch(self, index):
164
-
164
+
165
- self.keys[index].count_up()
165
+ self.keys[index].push()
166
166
 
167
167
 
168
168
 
@@ -188,7 +188,7 @@
188
188
 
189
189
  for i in range(8):
190
190
 
191
- game.count_up(i)
191
+ game.push_switch(i)
192
192
 
193
193
  time.sleep(1)
194
194
 

5

outline削除

2020/02/12 03:03

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -36,15 +36,9 @@
36
36
 
37
37
  self.canvas = canvas
38
38
 
39
- outer = x1, y1, x2, y2
40
-
41
- inner = x1 + 2, y1 + 2, x2 - 2, y2 - 2
42
-
43
- self.outline = shape(*outer)
44
-
45
- self.on = shape(*inner, fill='red')
39
+ self.on = shape(x1, y1, x2, y2, fill='red')
46
-
40
+
47
- self.off = shape(*inner, fill=color)
41
+ self.off = shape(x1, y1, x2, y2, fill=color)
48
42
 
49
43
  self.count = IntVar()
50
44
 

4

クラス名変更

2020/02/12 00:45

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
 
30
30
 
31
- class Shape:
31
+ class Switch:
32
32
 
33
33
 
34
34
 
@@ -74,7 +74,7 @@
74
74
 
75
75
 
76
76
 
77
- class Key(Shape):
77
+ class Key(Switch):
78
78
 
79
79
 
80
80
 
@@ -88,7 +88,7 @@
88
88
 
89
89
 
90
90
 
91
- class Disk(Shape):
91
+ class Disk(Switch):
92
92
 
93
93
 
94
94
 

3

コメント削除

2020/02/12 00:41

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -128,32 +128,22 @@
128
128
 
129
129
 
130
130
 
131
+ white_keys = [Key(canvas, x, y=195)
132
+
133
+ for x in range(280, 280 + 90 * 4, 90)]
134
+
135
+ black_keys = [Key(canvas, x, y=30, color='black')
136
+
137
+ for x in range(325, 325 + 90 * 3, 90)]
138
+
139
+ disk = Disk(canvas, x=35, y=65, size=220)
140
+
141
+ self.keys = [*white_keys, *black_keys, disk]
142
+
143
+
144
+
131
145
  font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
132
146
 
133
-
134
-
135
- # 白鍵盤
136
-
137
- white_keys = [Key(canvas, x, y=195)
138
-
139
- for x in range(280, 280 + 90 * 4, 90)]
140
-
141
- # 黒鍵盤
142
-
143
- black_keys = [Key(canvas, x, y=30, color='black')
144
-
145
- for x in range(325, 325 + 90 * 3, 90)]
146
-
147
- # 皿
148
-
149
- disk = Disk(canvas, x=35, y=65, size=220)
150
-
151
- self.keys = [*white_keys, *black_keys, disk]
152
-
153
-
154
-
155
- # カウンタ表示
156
-
157
147
  for i, key in enumerate(white_keys):
158
148
 
159
149
  label = tk.Label(window, font=font1, textvariable=key.count)

2

クラス名変更

2020/02/11 16:15

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -88,7 +88,7 @@
88
88
 
89
89
 
90
90
 
91
- class Circle(Shape):
91
+ class Disk(Shape):
92
92
 
93
93
 
94
94
 
@@ -146,7 +146,7 @@
146
146
 
147
147
  # 皿
148
148
 
149
- disk = Circle(canvas, x=35, y=65, size=220)
149
+ disk = Disk(canvas, x=35, y=65, size=220)
150
150
 
151
151
  self.keys = [*white_keys, *black_keys, disk]
152
152
 

1

サンプルコード追記

2020/02/11 09:44

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1 +1,225 @@
1
1
  色を戻すのは、tk部品の afterを使えばいいですよ。
2
+
3
+
4
+
5
+ tkinterだけの処理を書いてみました。
6
+
7
+ 残念ながら、スレッド終了がうまくいかなくてコマンド終了できませんけど、動作確認はできます。
8
+
9
+
10
+
11
+ ```python
12
+
13
+ import os
14
+
15
+ import sys
16
+
17
+ import time
18
+
19
+ import threading
20
+
21
+ os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
22
+
23
+ import tkinter as tk
24
+
25
+ from tkinter import font, IntVar
26
+
27
+
28
+
29
+
30
+
31
+ class Shape:
32
+
33
+
34
+
35
+ def __init__(self, canvas, shape, x1, y1, x2, y2, color):
36
+
37
+ self.canvas = canvas
38
+
39
+ outer = x1, y1, x2, y2
40
+
41
+ inner = x1 + 2, y1 + 2, x2 - 2, y2 - 2
42
+
43
+ self.outline = shape(*outer)
44
+
45
+ self.on = shape(*inner, fill='red')
46
+
47
+ self.off = shape(*inner, fill=color)
48
+
49
+ self.count = IntVar()
50
+
51
+
52
+
53
+ def count_up(self):
54
+
55
+ self.count.set(self.count.get() + 1)
56
+
57
+ self.light_on()
58
+
59
+
60
+
61
+ def light_on(self):
62
+
63
+ self.canvas.tag_raise(self.on, self.off)
64
+
65
+ self.canvas.after(300, self.light_off)
66
+
67
+
68
+
69
+ def light_off(self):
70
+
71
+ self.canvas.tag_raise(self.off, self.on)
72
+
73
+
74
+
75
+
76
+
77
+ class Key(Shape):
78
+
79
+
80
+
81
+ def __init__(self, canvas, x, y, width=70, height=135, color='white'):
82
+
83
+ super().__init__(canvas, canvas.create_rectangle,
84
+
85
+ x, y, x + width, y + height, color)
86
+
87
+
88
+
89
+
90
+
91
+ class Circle(Shape):
92
+
93
+
94
+
95
+ def __init__(self, canvas, x, y, size, color='white'):
96
+
97
+ super().__init__(canvas, canvas.create_oval,
98
+
99
+ x, y, x + size, y + size, color)
100
+
101
+
102
+
103
+
104
+
105
+ class Game:
106
+
107
+
108
+
109
+ def __init__(self, title):
110
+
111
+ self.title = title
112
+
113
+
114
+
115
+ def run(self):
116
+
117
+ self.window = window = tk.Tk()
118
+
119
+ window.title(self.title)
120
+
121
+ window.geometry('640x360')
122
+
123
+
124
+
125
+ canvas = tk.Canvas(window, width=640, height=360)
126
+
127
+ canvas.place(x=0, y=0)
128
+
129
+
130
+
131
+ font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
132
+
133
+
134
+
135
+ # 白鍵盤
136
+
137
+ white_keys = [Key(canvas, x, y=195)
138
+
139
+ for x in range(280, 280 + 90 * 4, 90)]
140
+
141
+ # 黒鍵盤
142
+
143
+ black_keys = [Key(canvas, x, y=30, color='black')
144
+
145
+ for x in range(325, 325 + 90 * 3, 90)]
146
+
147
+ # 皿
148
+
149
+ disk = Circle(canvas, x=35, y=65, size=220)
150
+
151
+ self.keys = [*white_keys, *black_keys, disk]
152
+
153
+
154
+
155
+ # カウンタ表示
156
+
157
+ for i, key in enumerate(white_keys):
158
+
159
+ label = tk.Label(window, font=font1, textvariable=key.count)
160
+
161
+ label.place(x=310 + 90 * i, y=333)
162
+
163
+ for i, key in enumerate(black_keys):
164
+
165
+ label = tk.Label(window, font=font1, textvariable=key.count)
166
+
167
+ label.place(x=355 + 90 * i, y=5)
168
+
169
+ label = tk.Label(window, font=font1, textvariable=disk.count)
170
+
171
+ label.place(x=205, y=300)
172
+
173
+
174
+
175
+ self.window.mainloop()
176
+
177
+
178
+
179
+ def count_up(self, index):
180
+
181
+ self.keys[index].count_up()
182
+
183
+
184
+
185
+ def quit(self):
186
+
187
+ self.window.destroy()
188
+
189
+
190
+
191
+
192
+
193
+ def main():
194
+
195
+ game = Game('Contoller input')
196
+
197
+ thread = threading.Thread(target=game.run)
198
+
199
+ thread.start()
200
+
201
+
202
+
203
+ time.sleep(1)
204
+
205
+ for i in range(8):
206
+
207
+ game.count_up(i)
208
+
209
+ time.sleep(1)
210
+
211
+
212
+
213
+ game.quit()
214
+
215
+ thread.join()
216
+
217
+ exit()
218
+
219
+
220
+
221
+ if __name__ == '__main__':
222
+
223
+ main()
224
+
225
+ ```