Tkinterでpygameを使って音を出すときの注意点
たとえばこんな感じで,単に音を鳴らすだけのウィンドウを作ったとして,
import tkinter as tk import pygame pygame.init() pygame.mixer.init() se = pygame.mixer.Sound('./se_file.wav') root = tk.Tk() root.title('hoge') def ring(): se.play() button = tk.Button(root, command=ring) button.pack() root.mainloop()
以下のようなエラーが出て起動しないことがある.
pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html 2019-05-31 13:15:16.969 python[5856:443232] 13:15:16.968 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h. 2019-05-31 13:15:17.033 python[5856:443232] -[PYGSDLApplication _setup:]: unrecognized selector sent to instance 0x7f8682f5ccd0 2019-05-31 13:15:17.034 python[5856:443232] An uncaught exception was raised 2019-05-31 13:15:17.034 python[5856:443232] -[PYGSDLApplication _setup:]: unrecognized selector sent to instance 0x7f8682f5ccd0 2019-05-31 13:15:17.034 python[5856:443232] ( 0 CoreFoundation 0x00007fff93f1e452 __exceptionPreprocess + 178 1 libobjc.A.dylib 0x00007fff8b16ef7e objc_exception_throw + 48 2 CoreFoundation 0x00007fff93f8818d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x00007fff93e8e4c1 ___forwarding___ + 1009 4 CoreFoundation 0x00007fff93e8e048 _CF_forwarding_prep_0 + 120 5 Tk 0x0000000102478948 TkpInit + 476 6 Tk 0x00000001023f3a6e Tk_Init + 1799 7 _tkinter.cpython-36m-darwin.so 0x00000001022d0da4 Tcl_AppInit + 84 8 _tkinter.cpython-36m-darwin.so 0x00000001022d0a7b _tkinter_create + 1115 9 libpython3.6m.dylib 0x0000000101a32a81 _PyCFunction_FastCallDict + 497 10 libpython3.6m.dylib 0x0000000101ab4787 call_function + 439 11 libpython3.6m.dylib 0x0000000101ab0f62 _PyEval_EvalFrameDefault + 27346 12 libpython3.6m.dylib 0x0000000101ab51ef _PyEval_EvalCodeWithName + 2447 13 libpython3.6m.dylib 0x0000000101ab5d92 _PyFunction_FastCallDict + 738 14 libpython3.6m.dylib 0x00000001019ea677 _PyObject_FastCallDict + 247 15 libpython3.6m.dylib 0x00000001019ea795 _PyObject_Call_Prepend + 149 16 libpython3.6m.dylib 0x00000001019ea4b0 PyObject_Call + 96 17 libpython3.6m.dylib 0x0000000101a4aaed slot_tp_init + 125 18 libpython3.6m.dylib 0x0000000101a46e29 type_call + 313 19 libpython3.6m.dylib 0x00000001019ea645 _PyObject_FastCallDict + 197 20 libpython3.6m.dylib 0x0000000101ab4688 call_function + 184 21 libpython3.6m.dylib 0x0000000101ab0f62 _PyEval_EvalFrameDefault + 27346 22 libpython3.6m.dylib 0x0000000101ab51ef _PyEval_EvalCodeWithName + 2447 23 libpython3.6m.dylib 0x0000000101aaa3d4 PyEval_EvalCode + 100 24 libpython3.6m.dylib 0x0000000101ae00f1 PyRun_FileExFlags + 209 25 libpython3.6m.dylib 0x0000000101adf8a3 PyRun_SimpleFileExFlags + 851 26 libpython3.6m.dylib 0x0000000101af870f Py_Main + 3535 27 python 0x00000001019d5df8 main + 232 28 libdyld.dylib 0x00007fff8c0cb5ad start + 1 ) 2019-05-31 13:15:17.162 python[5856:443232] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PYGSDLApplication _setup:]: unrecognized selector sent to instance 0x7f8682f5ccd0' *** First throw call stack: ( 0 CoreFoundation 0x00007fff93f1e452 __exceptionPreprocess + 178 1 libobjc.A.dylib 0x00007fff8b16ef7e objc_exception_throw + 48 2 CoreFoundation 0x00007fff93f8818d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x00007fff93e8e4c1 ___forwarding___ + 1009 4 CoreFoundation 0x00007fff93e8e048 _CF_forwarding_prep_0 + 120 5 Tk 0x0000000102478948 TkpInit + 476 6 Tk 0x00000001023f3a6e Tk_Init + 1799 7 _tkinter.cpython-36m-darwin.so 0x00000001022d0da4 Tcl_AppInit + 84 8 _tkinter.cpython-36m-darwin.so 0x00000001022d0a7b _tkinter_create + 1115 9 libpython3.6m.dylib 0x0000000101a32a81 _PyCFunction_FastCallDict + 497 10 libpython3.6m.dylib 0x0000000101ab4787 call_function + 439 11 libpython3.6m.dylib 0x0000000101ab0f62 _PyEval_EvalFrameDefault + 27346 12 libpython3.6m.dylib 0x0000000101ab51ef _PyEval_EvalCodeWithName + 2447 13 libpython3.6m.dylib 0x0000000101ab5d92 _PyFunction_FastCallDict + 738 14 libpython3.6m.dylib 0x00000001019ea677 _PyObject_FastCallDict + 247 15 libpython3.6m.dylib 0x00000001019ea795 _PyObject_Call_Prepend + 149 16 libpython3.6m.dylib 0x00000001019ea4b0 PyObject_Call + 96 17 libpython3.6m.dylib 0x0000000101a4aaed slot_tp_init + 125 18 libpython3.6m.dylib 0x0000000101a46e29 type_call + 313 19 libpython3.6m.dylib 0x00000001019ea645 _PyObject_FastCallDict + 197 20 libpython3.6m.dylib 0x0000000101ab4688 call_function + 184 21 libpython3.6m.dylib 0x0000000101ab0f62 _PyEval_EvalFrameDefault + 27346 22 libpython3.6m.dylib 0x0000000101ab51ef _PyEval_EvalCodeWithName + 2447 23 libpython3.6m.dylib 0x0000000101aaa3d4 PyEval_EvalCode + 100 24 libpython3.6m.dylib 0x0000000101ae00f1 PyRun_FileExFlags + 209 25 libpython3.6m.dylib 0x0000000101adf8a3 PyRun_SimpleFileExFlags + 851 26 libpython3.6m.dylib 0x0000000101af870f Py_Main + 3535 27 python 0x00000001019d5df8 main + 232 28 libdyld.dylib 0x00007fff8c0cb5ad start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException Abort trap: 6
私の環境だとなぜかElpyでC-c C-c
だと実行できるが,シェルからだと起動できなかった.
解決は以下.
stackoverflow.com
import tkinter as tk import pygame root = tk.Tk() root.title('hoge') pygame.init() pygame.mixer.init() se = pygame.mixer.Sound('./se_file.wav') def ring(): se.play() button = tk.Button(root, command=ring) button.pack() root.mainloop()
pygame.init()
のまとまりをroot = tk.Tk()
のまとまりの後に移動したら解決した.