teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

15

pygame連携について追記

2020/02/16 09:54

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -116,4 +116,69 @@
116
116
 
117
117
  if __name__ == '__main__':
118
118
  main()
119
+ ```
120
+
121
+ pygameとの連携がうまく動かないということなら、以下のような単純化した処理に置き換えて動作核にしてみてはいかがですか?
122
+
123
+ ```py
124
+ import os
125
+ import sys
126
+ import time
127
+ import threading
128
+ import pygame
129
+ from pygame.locals import *
130
+ os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
131
+ import tkinter as tk
132
+ from tkinter import font, IntVar
133
+
134
+
135
+ class Game:
136
+
137
+ def __init__(self, title):
138
+ self.title = title
139
+
140
+ def run(self):
141
+ self.running = True
142
+ while self.running:
143
+ time.sleep(1)
144
+
145
+ def push_switch(self, number):
146
+ print("push_switch:", repr(number))
147
+
148
+ def quit(self):
149
+ self.running = False
150
+
151
+
152
+ def main():
153
+ game = Game('Contoller input')
154
+ thread = threading.Thread(target=game.run)
155
+ thread.start()
156
+ pygame.joystick.init()
157
+ pygame.init()
158
+
159
+ try:
160
+ j = pygame.joystick.Joystick(0) # create a joystick instance
161
+ j.init() # init instance
162
+ print(j.get_name())
163
+ except pygame.error:
164
+ print('Joystickが見つかりませんでした。')
165
+ game.quit()
166
+
167
+ for e in pygame.event.get():
168
+ print(e)
169
+ if(e.type == pygame.locals.JOYBUTTONDOWN or e.type == pygame.locals.JOYAXISMOTION or e.type == QUIT):
170
+ if(e.type == QUIT):
171
+ print("quit")
172
+ game.quit()
173
+ thread.join()
174
+ if(e.type == pygame.locals.JOYBUTTONDOWN):
175
+ print("button " + str(e.button))
176
+ if(e.button < 7):
177
+ game.push_switch(e.button)
178
+ elif(e.type == pygame.locals.JOYAXISMOTION):
179
+ print("turntable spin")
180
+ game.push_switch(7)
181
+
182
+ if __name__ == '__main__':
183
+ main()
119
184
  ```

14

スイッチ番号変更

2020/02/16 09:54

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -54,11 +54,20 @@
54
54
 
55
55
  def run(self):
56
56
  self.running = True
57
- self.window = window = tk.Tk()
57
+ self.window = tk.Tk()
58
- window.title(self.title)
58
+ self.window.title(self.title)
59
- window.geometry('640x360')
59
+ self.window.geometry('640x360')
60
60
 
61
+ self.switches = self._make_switches()
62
+
63
+ self.window.after(1000, self._check_to_quit)
64
+ self.window.mainloop()
65
+ # need to delete variables that reference tkinter objects in the thread
66
+ del self.switches
67
+ del self.window
68
+
69
+ def _make_switches(self):
61
- canvas = tk.Canvas(window, width=640, height=360)
70
+ canvas = tk.Canvas(self.window, width=640, height=360)
62
71
  canvas.place(x=0, y=0)
63
72
 
64
73
  white_keys = [Key(canvas, x, y=195)
@@ -69,28 +78,23 @@
69
78
 
70
79
  font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
71
80
  for i, key in enumerate(white_keys):
72
- label = tk.Label(window, font=font1, textvariable=key.count)
81
+ label = tk.Label(self.window, font=font1, textvariable=key.count)
73
82
  label.place(x=310 + 90 * i, y=333)
74
83
  for i, key in enumerate(black_keys):
75
- label = tk.Label(window, font=font1, textvariable=key.count)
84
+ label = tk.Label(self.window, font=font1, textvariable=key.count)
76
85
  label.place(x=355 + 90 * i, y=5)
77
- label = tk.Label(window, font=font1, textvariable=disk.count)
86
+ label = tk.Label(self.window, font=font1, textvariable=disk.count)
78
87
  label.place(x=205, y=300)
79
88
 
80
- self.keys = dict(zip((0, 2, 4, 6, 1, 3, 5, 7),
89
+ return dict(zip((0, 2, 4, 6, 1, 3, 5, 7),
81
- (*white_keys, *black_keys, disk)))
90
+ (*white_keys, *black_keys, disk)))
82
- self.window.after(1000, self.check_to_quit)
83
- self.window.mainloop()
84
- # need to delete variables that reference tkinter objects
85
- del self.keys
86
- del self.window
87
91
 
88
92
  def push_switch(self, index):
89
- self.keys[index].push()
93
+ self.switches[index].push()
90
94
 
91
- def check_to_quit(self):
95
+ def _check_to_quit(self):
92
96
  if self.running:
93
- self.window.after(1000, self.check_to_quit)
97
+ self.window.after(1000, self._check_to_quit)
94
98
  else:
95
99
  self.window.destroy()
96
100
 

13

ボタン順序変更

2020/02/12 08:35

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -66,7 +66,6 @@
66
66
  black_keys = [Key(canvas, x, y=30, color='black')
67
67
  for x in range(325, 325 + 90 * 3, 90)]
68
68
  disk = Disk(canvas, x=35, y=65, size=220)
69
- self.keys = [*white_keys, *black_keys, disk]
70
69
 
71
70
  font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
72
71
  for i, key in enumerate(white_keys):
@@ -78,6 +77,8 @@
78
77
  label = tk.Label(window, font=font1, textvariable=disk.count)
79
78
  label.place(x=205, y=300)
80
79
 
80
+ self.keys = dict(zip((0, 2, 4, 6, 1, 3, 5, 7),
81
+ (*white_keys, *black_keys, disk)))
81
82
  self.window.after(1000, self.check_to_quit)
82
83
  self.window.mainloop()
83
84
  # need to delete variables that reference tkinter objects

12

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

2020/02/12 06:02

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -80,6 +80,7 @@
80
80
 
81
81
  self.window.after(1000, self.check_to_quit)
82
82
  self.window.mainloop()
83
+ # need to delete variables that reference tkinter objects
83
84
  del self.keys
84
85
  del self.window
85
86
 

11

exit()削除

2020/02/12 04:39

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -107,7 +107,6 @@
107
107
 
108
108
  game.quit()
109
109
  thread.join()
110
- exit()
111
110
 
112
111
  if __name__ == '__main__':
113
112
  main()

10

終了処理変更

2020/02/12 04:28

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -80,6 +80,8 @@
80
80
 
81
81
  self.window.after(1000, self.check_to_quit)
82
82
  self.window.mainloop()
83
+ del self.keys
84
+ del self.window
83
85
 
84
86
  def push_switch(self, index):
85
87
  self.keys[index].push()
@@ -89,8 +91,6 @@
89
91
  self.window.after(1000, self.check_to_quit)
90
92
  else:
91
93
  self.window.destroy()
92
- del self.keys
93
- del self.window
94
94
 
95
95
  def quit(self):
96
96
  self.running = False
@@ -98,7 +98,6 @@
98
98
  def main():
99
99
  game = Game('Contoller input')
100
100
  thread = threading.Thread(target=game.run)
101
- thread.setDaemon(True)
102
101
  thread.start()
103
102
 
104
103
  time.sleep(1)

9

終了処理変更

2020/02/12 04:25

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -88,9 +88,9 @@
88
88
  if self.running:
89
89
  self.window.after(1000, self.check_to_quit)
90
90
  else:
91
- self.keys = None
92
91
  self.window.destroy()
92
+ del self.keys
93
- self.window = None
93
+ del self.window
94
94
 
95
95
  def quit(self):
96
96
  self.running = False

8

正常終了するように修正

2020/02/12 03:30

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  色を戻すのは、tk部品の afterを使えばいいですよ。
2
2
 
3
3
  tkinterだけの処理を書いてみました。
4
- 残念ながら、スレッド終了がうまくいかなくてコマンド終了できませんど、動作確認はできます
4
+ このプログラムだ動作確認してみてください
5
5
 
6
6
  ```python
7
7
  import os
@@ -80,7 +80,6 @@
80
80
 
81
81
  self.window.after(1000, self.check_to_quit)
82
82
  self.window.mainloop()
83
- print("exit mainloop()")
84
83
 
85
84
  def push_switch(self, index):
86
85
  self.keys[index].push()
@@ -91,6 +90,7 @@
91
90
  else:
92
91
  self.keys = None
93
92
  self.window.destroy()
93
+ self.window = None
94
94
 
95
95
  def quit(self):
96
96
  self.running = False

7

quit処理変更

2020/02/12 03:26

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -53,6 +53,7 @@
53
53
  self.title = title
54
54
 
55
55
  def run(self):
56
+ self.running = True
56
57
  self.window = window = tk.Tk()
57
58
  window.title(self.title)
58
59
  window.geometry('640x360')
@@ -77,18 +78,27 @@
77
78
  label = tk.Label(window, font=font1, textvariable=disk.count)
78
79
  label.place(x=205, y=300)
79
80
 
81
+ self.window.after(1000, self.check_to_quit)
80
82
  self.window.mainloop()
83
+ print("exit mainloop()")
81
84
 
82
85
  def push_switch(self, index):
83
86
  self.keys[index].push()
84
87
 
88
+ def check_to_quit(self):
89
+ if self.running:
90
+ self.window.after(1000, self.check_to_quit)
91
+ else:
92
+ self.keys = None
93
+ self.window.destroy()
94
+
85
95
  def quit(self):
86
- self.window.destroy()
96
+ self.running = False
87
97
 
88
-
89
98
  def main():
90
99
  game = Game('Contoller input')
91
100
  thread = threading.Thread(target=game.run)
101
+ thread.setDaemon(True)
92
102
  thread.start()
93
103
 
94
104
  time.sleep(1)

6

メソッド名変更

2020/02/12 03:23

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -21,7 +21,7 @@
21
21
  self.off = shape(x1, y1, x2, y2, fill=color)
22
22
  self.count = IntVar()
23
23
 
24
- def count_up(self):
24
+ def push(self):
25
25
  self.count.set(self.count.get() + 1)
26
26
  self.light_on()
27
27
 
@@ -79,8 +79,8 @@
79
79
 
80
80
  self.window.mainloop()
81
81
 
82
- def count_up(self, index):
82
+ def push_switch(self, index):
83
- self.keys[index].count_up()
83
+ self.keys[index].push()
84
84
 
85
85
  def quit(self):
86
86
  self.window.destroy()
@@ -93,7 +93,7 @@
93
93
 
94
94
  time.sleep(1)
95
95
  for i in range(8):
96
- game.count_up(i)
96
+ game.push_switch(i)
97
97
  time.sleep(1)
98
98
 
99
99
  game.quit()

5

outline削除

2020/02/12 03:03

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -17,11 +17,8 @@
17
17
 
18
18
  def __init__(self, canvas, shape, x1, y1, x2, y2, color):
19
19
  self.canvas = canvas
20
- outer = x1, y1, x2, y2
21
- inner = x1 + 2, y1 + 2, x2 - 2, y2 - 2
22
- self.outline = shape(*outer)
23
- self.on = shape(*inner, fill='red')
20
+ self.on = shape(x1, y1, x2, y2, fill='red')
24
- self.off = shape(*inner, fill=color)
21
+ self.off = shape(x1, y1, x2, y2, fill=color)
25
22
  self.count = IntVar()
26
23
 
27
24
  def count_up(self):

4

クラス名変更

2020/02/12 00:45

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -13,7 +13,7 @@
13
13
  from tkinter import font, IntVar
14
14
 
15
15
 
16
- class Shape:
16
+ class Switch:
17
17
 
18
18
  def __init__(self, canvas, shape, x1, y1, x2, y2, color):
19
19
  self.canvas = canvas
@@ -36,14 +36,14 @@
36
36
  self.canvas.tag_raise(self.off, self.on)
37
37
 
38
38
 
39
- class Key(Shape):
39
+ class Key(Switch):
40
40
 
41
41
  def __init__(self, canvas, x, y, width=70, height=135, color='white'):
42
42
  super().__init__(canvas, canvas.create_rectangle,
43
43
  x, y, x + width, y + height, color)
44
44
 
45
45
 
46
- class Disk(Shape):
46
+ class Disk(Switch):
47
47
 
48
48
  def __init__(self, canvas, x, y, size, color='white'):
49
49
  super().__init__(canvas, canvas.create_oval,

3

コメント削除

2020/02/12 00:41

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -63,19 +63,14 @@
63
63
  canvas = tk.Canvas(window, width=640, height=360)
64
64
  canvas.place(x=0, y=0)
65
65
 
66
- font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
67
-
68
- # 白鍵盤
69
66
  white_keys = [Key(canvas, x, y=195)
70
67
  for x in range(280, 280 + 90 * 4, 90)]
71
- # 黒鍵盤
72
68
  black_keys = [Key(canvas, x, y=30, color='black')
73
69
  for x in range(325, 325 + 90 * 3, 90)]
74
- # 皿
75
70
  disk = Disk(canvas, x=35, y=65, size=220)
76
71
  self.keys = [*white_keys, *black_keys, disk]
77
72
 
78
- # カウンタ表示
73
+ font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
79
74
  for i, key in enumerate(white_keys):
80
75
  label = tk.Label(window, font=font1, textvariable=key.count)
81
76
  label.place(x=310 + 90 * i, y=333)

2

クラス名変更

2020/02/11 16:15

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -43,7 +43,7 @@
43
43
  x, y, x + width, y + height, color)
44
44
 
45
45
 
46
- class Circle(Shape):
46
+ class Disk(Shape):
47
47
 
48
48
  def __init__(self, canvas, x, y, size, color='white'):
49
49
  super().__init__(canvas, canvas.create_oval,
@@ -72,7 +72,7 @@
72
72
  black_keys = [Key(canvas, x, y=30, color='black')
73
73
  for x in range(325, 325 + 90 * 3, 90)]
74
74
  # 皿
75
- disk = Circle(canvas, x=35, y=65, size=220)
75
+ disk = Disk(canvas, x=35, y=65, size=220)
76
76
  self.keys = [*white_keys, *black_keys, disk]
77
77
 
78
78
  # カウンタ表示

1

サンプルコード追記

2020/02/11 09:44

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -1,1 +1,113 @@
1
- 色を戻すのは、tk部品の afterを使えばいいですよ。
1
+ 色を戻すのは、tk部品の afterを使えばいいですよ。
2
+
3
+ tkinterだけの処理を書いてみました。
4
+ 残念ながら、スレッド終了がうまくいかなくてコマンド終了できませんけど、動作確認はできます。
5
+
6
+ ```python
7
+ import os
8
+ import sys
9
+ import time
10
+ import threading
11
+ os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
12
+ import tkinter as tk
13
+ from tkinter import font, IntVar
14
+
15
+
16
+ class Shape:
17
+
18
+ def __init__(self, canvas, shape, x1, y1, x2, y2, color):
19
+ self.canvas = canvas
20
+ outer = x1, y1, x2, y2
21
+ inner = x1 + 2, y1 + 2, x2 - 2, y2 - 2
22
+ self.outline = shape(*outer)
23
+ self.on = shape(*inner, fill='red')
24
+ self.off = shape(*inner, fill=color)
25
+ self.count = IntVar()
26
+
27
+ def count_up(self):
28
+ self.count.set(self.count.get() + 1)
29
+ self.light_on()
30
+
31
+ def light_on(self):
32
+ self.canvas.tag_raise(self.on, self.off)
33
+ self.canvas.after(300, self.light_off)
34
+
35
+ def light_off(self):
36
+ self.canvas.tag_raise(self.off, self.on)
37
+
38
+
39
+ class Key(Shape):
40
+
41
+ def __init__(self, canvas, x, y, width=70, height=135, color='white'):
42
+ super().__init__(canvas, canvas.create_rectangle,
43
+ x, y, x + width, y + height, color)
44
+
45
+
46
+ class Circle(Shape):
47
+
48
+ def __init__(self, canvas, x, y, size, color='white'):
49
+ super().__init__(canvas, canvas.create_oval,
50
+ x, y, x + size, y + size, color)
51
+
52
+
53
+ class Game:
54
+
55
+ def __init__(self, title):
56
+ self.title = title
57
+
58
+ def run(self):
59
+ self.window = window = tk.Tk()
60
+ window.title(self.title)
61
+ window.geometry('640x360')
62
+
63
+ canvas = tk.Canvas(window, width=640, height=360)
64
+ canvas.place(x=0, y=0)
65
+
66
+ font1 = font.Font(family='Bauhaus93', size=10, weight='bold')
67
+
68
+ # 白鍵盤
69
+ white_keys = [Key(canvas, x, y=195)
70
+ for x in range(280, 280 + 90 * 4, 90)]
71
+ # 黒鍵盤
72
+ black_keys = [Key(canvas, x, y=30, color='black')
73
+ for x in range(325, 325 + 90 * 3, 90)]
74
+ # 皿
75
+ disk = Circle(canvas, x=35, y=65, size=220)
76
+ self.keys = [*white_keys, *black_keys, disk]
77
+
78
+ # カウンタ表示
79
+ for i, key in enumerate(white_keys):
80
+ label = tk.Label(window, font=font1, textvariable=key.count)
81
+ label.place(x=310 + 90 * i, y=333)
82
+ for i, key in enumerate(black_keys):
83
+ label = tk.Label(window, font=font1, textvariable=key.count)
84
+ label.place(x=355 + 90 * i, y=5)
85
+ label = tk.Label(window, font=font1, textvariable=disk.count)
86
+ label.place(x=205, y=300)
87
+
88
+ self.window.mainloop()
89
+
90
+ def count_up(self, index):
91
+ self.keys[index].count_up()
92
+
93
+ def quit(self):
94
+ self.window.destroy()
95
+
96
+
97
+ def main():
98
+ game = Game('Contoller input')
99
+ thread = threading.Thread(target=game.run)
100
+ thread.start()
101
+
102
+ time.sleep(1)
103
+ for i in range(8):
104
+ game.count_up(i)
105
+ time.sleep(1)
106
+
107
+ game.quit()
108
+ thread.join()
109
+ exit()
110
+
111
+ if __name__ == '__main__':
112
+ main()
113
+ ```