質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -295,3 +295,281 @@
|
|
295
295
|
|
296
296
|
|
297
297
|
ligntning関数を用いてボタンを光らせているかの判定をしたく、ボタンを押してから0.3秒間はGUIの該当部分を赤くするようにしたいのですが、現状では図形が上の画像のまま変色しません。どこを改善したらよいかご教授お願い致します。
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
# 追記
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
shiracamusさんの記述を参考にしたのですが、main関数にて記述したpygameの部分が認識されません。ターミナルにもprintで来ていない状態です。
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
```python
|
312
|
+
|
313
|
+
import os
|
314
|
+
|
315
|
+
import sys
|
316
|
+
|
317
|
+
import time
|
318
|
+
|
319
|
+
import threading
|
320
|
+
|
321
|
+
import pygame
|
322
|
+
|
323
|
+
from pygame.locals import *
|
324
|
+
|
325
|
+
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
|
326
|
+
|
327
|
+
import tkinter as tk
|
328
|
+
|
329
|
+
from tkinter import font, IntVar
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
class Switch:
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
def __init__(self, canvas, shape, x1, y1, x2, y2, color):
|
340
|
+
|
341
|
+
self.canvas = canvas
|
342
|
+
|
343
|
+
self.on = shape(x1, y1, x2, y2, fill='red')
|
344
|
+
|
345
|
+
self.off = shape(x1, y1, x2, y2, fill=color)
|
346
|
+
|
347
|
+
self.count = IntVar()
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
def push(self):
|
352
|
+
|
353
|
+
self.count.set(self.count.get() + 1)
|
354
|
+
|
355
|
+
self.light_on()
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
def light_on(self):
|
360
|
+
|
361
|
+
self.canvas.tag_raise(self.on, self.off)
|
362
|
+
|
363
|
+
self.canvas.after(300, self.light_off)
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
def light_off(self):
|
368
|
+
|
369
|
+
self.canvas.tag_raise(self.off, self.on)
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
class Key(Switch):
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
def __init__(self, canvas, x, y, width=70, height=135, color='white'):
|
380
|
+
|
381
|
+
super().__init__(canvas, canvas.create_rectangle,
|
382
|
+
|
383
|
+
x, y, x + width, y + height, color)
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
class Disk(Switch):
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
def __init__(self, canvas, x, y, size, color='white'):
|
394
|
+
|
395
|
+
super().__init__(canvas, canvas.create_oval,
|
396
|
+
|
397
|
+
x, y, x + size, y + size, color)
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
class Game:
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
def __init__(self, title):
|
408
|
+
|
409
|
+
self.title = title
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
def run(self):
|
414
|
+
|
415
|
+
self.running = True
|
416
|
+
|
417
|
+
self.window = tk.Tk()
|
418
|
+
|
419
|
+
self.window.title(self.title)
|
420
|
+
|
421
|
+
self.window.geometry('640x360')
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
self.switches = self._make_switches()
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
self.window.after(1000, self._check_to_quit)
|
430
|
+
|
431
|
+
self.window.mainloop()
|
432
|
+
|
433
|
+
# need to delete variables that reference tkinter objects in the thread
|
434
|
+
|
435
|
+
del self.switches
|
436
|
+
|
437
|
+
del self.window
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
def _make_switches(self):
|
442
|
+
|
443
|
+
canvas = tk.Canvas(self.window, width=640, height=360)
|
444
|
+
|
445
|
+
canvas.place(x=0, y=0)
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
white_keys = [Key(canvas, x, y=195)
|
450
|
+
|
451
|
+
for x in range(280, 280 + 90 * 4, 90)]
|
452
|
+
|
453
|
+
black_keys = [Key(canvas, x, y=30, color='black')
|
454
|
+
|
455
|
+
for x in range(325, 325 + 90 * 3, 90)]
|
456
|
+
|
457
|
+
disk = Disk(canvas, x=35, y=65, size=220)
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
|
462
|
+
|
463
|
+
for i, key in enumerate(white_keys):
|
464
|
+
|
465
|
+
label = tk.Label(self.window, font=font1, textvariable=key.count)
|
466
|
+
|
467
|
+
label.place(x=310 + 90 * i, y=333)
|
468
|
+
|
469
|
+
for i, key in enumerate(black_keys):
|
470
|
+
|
471
|
+
label = tk.Label(self.window, font=font1, textvariable=key.count)
|
472
|
+
|
473
|
+
label.place(x=355 + 90 * i, y=5)
|
474
|
+
|
475
|
+
label = tk.Label(self.window, font=font1, textvariable=disk.count)
|
476
|
+
|
477
|
+
label.place(x=205, y=300)
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
return dict(zip((0, 2, 4, 6, 1, 3, 5, 7),
|
482
|
+
|
483
|
+
(*white_keys, *black_keys, disk)))
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
def push_switch(self, index):
|
488
|
+
|
489
|
+
self.switches[index].push()
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
def _check_to_quit(self):
|
494
|
+
|
495
|
+
if self.running:
|
496
|
+
|
497
|
+
self.window.after(1000, self._check_to_quit)
|
498
|
+
|
499
|
+
else:
|
500
|
+
|
501
|
+
self.window.destroy()
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
def quit(self):
|
506
|
+
|
507
|
+
self.running = False
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
def main():
|
512
|
+
|
513
|
+
game = Game('Contoller input')
|
514
|
+
|
515
|
+
thread = threading.Thread(target=game.run)
|
516
|
+
|
517
|
+
thread.start()
|
518
|
+
|
519
|
+
pygame.joystick.init()
|
520
|
+
|
521
|
+
pygame.init()
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
try:
|
526
|
+
|
527
|
+
j = pygame.joystick.Joystick(0) # create a joystick instance
|
528
|
+
|
529
|
+
j.init() # init instance
|
530
|
+
|
531
|
+
print(j.get_name())
|
532
|
+
|
533
|
+
except pygame.error:
|
534
|
+
|
535
|
+
print('Joystickが見つかりませんでした。')
|
536
|
+
|
537
|
+
game.quit()
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
for e in pygame.event.get():
|
542
|
+
|
543
|
+
print(e)
|
544
|
+
|
545
|
+
if(e.type == pygame.locals.JOYBUTTONDOWN or e.type == pygame.locals.JOYAXISMOTION or e.type == QUIT):
|
546
|
+
|
547
|
+
if(e.type == QUIT):
|
548
|
+
|
549
|
+
print("quit")
|
550
|
+
|
551
|
+
game.quit()
|
552
|
+
|
553
|
+
thread.join()
|
554
|
+
|
555
|
+
if(e.type == pygame.locals.JOYBUTTONDOWN):
|
556
|
+
|
557
|
+
print("button " + str(e.button))
|
558
|
+
|
559
|
+
if(e.button < 7):
|
560
|
+
|
561
|
+
game.push_switch(e.button)
|
562
|
+
|
563
|
+
elif(e.type == pygame.locals.JOYAXISMOTION):
|
564
|
+
|
565
|
+
print("turntable spin")
|
566
|
+
|
567
|
+
game.push_switch(7)
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
if __name__ == '__main__':
|
572
|
+
|
573
|
+
main()
|
574
|
+
|
575
|
+
```
|