pythonでGUIアプリを作るためにkivyの勉強をしています。
キー入力をとりたいのですが、公式のサンプルコードを実行してもキー入
力を取ることができませんでした。どのようにすれば入力を取ることができるのでしょうか。
このサンプルコードを実行してキーを押しても"The key..."という文字列が表示されません。
OSはMacOSで、pythonのバージョンは3.6.0です。
python
1import kivy 2kivy.require('1.0.8') 3 4from kivy.core.window import Window 5from kivy.uix.widget import Widget 6from kivy.modules import keybinding 7 8class MyKeyboardListener(Widget): 9 10 def __init__(self, **kwargs): 11 super(MyKeyboardListener, self).__init__(**kwargs) 12 self._keyboard = Window.request_keyboard( 13 self._keyboard_closed, self, 'text') 14 if self._keyboard.widget: 15 # If it exists, this widget is a VKeyboard object which you can use 16 # to change the keyboard layout. 17 pass 18 self._keyboard.bind(on_key_down=self._on_keyboard_down) 19 20 def _keyboard_closed(self): 21 print('My keyboard have been closed!') 22 self._keyboard.unbind(on_key_down=self._on_keyboard_down) 23 self._keyboard = None 24 25 def _on_keyboard_down(self, keyboard, keycode, text, modifiers): 26 print('The key', keycode, 'have been pressed') 27 print(' - text is %r' % text) 28 print(' - modifiers are %r' % modifiers) 29 30 # Keycode is composed of an integer + a string 31 # If we hit escape, release the keyboard 32 if keycode[1] == 'escape': 33 keyboard.release() 34 35 # Return True to accept the key. Otherwise, it will be used by 36 # the system. 37 return True 38 39 40if __name__ == '__main__': 41 from kivy.base import runTouchApp 42 runTouchApp(MyKeyboardListener())
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/22 10:59