kivyで画面遷移出来ません。
Q&A
解決したいこと
キーボード入力で画面を遷移させたいです。
manage.currentではダメなのでしょうか?
該当するソースコード
import kivy
kivy.require('1.0.8')
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.modules import keybinding
from kivy.properties import StringProperty, NumericProperty
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
import japanize_kivy
class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(
self._keyboard_closed, self, 'text')
if self._keyboard.widget:
# If it exists, this widget is a VKeyboard object which you can use
# to change the keyboard layout.
pass
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
print('subへ')
self.changer()
if keycode[1] == 'escape':
keyboard.release()
return True
def changer(self): # 画面遷移
self.manager.current = 'sub'
class SubScreen(Screen):
def __init__(self, **kwargs):
super(SubScreen, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(
self._keyboard_closed, self, 'text')
if self._keyboard.widget:
# If it exists, this widget is a VKeyboard object which you can use
# to change the keyboard layout.
pass
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
print('mainへ')
self.changer()
if keycode[1] == 'escape':
keyboard.release()
return True
def changer(self): # 画面遷移
self.manager.current = 'main'
class testApp(App):
def __init__(self, **kwargs):
super(testApp, self).__init__(**kwargs)
self.title = 'test'
self.sm = ''
def build(self):
# スクリーンに名前をつけて管理
self.sm = ScreenManager(transition=NoTransition())
self.sm.add_widget(MainScreen(name='main'))
self.sm.add_widget(SubScreen(name='sub'))
return self.sm
if __name__ == '__main__':
testApp().run()
0