【发布时间】:2021-10-25 17:24:38
【问题描述】:
基本上,我一直在开发具有多个屏幕的 Kivy 应用程序,并且我正在尝试制作它,以便在按下退出按钮时将您带回主屏幕。事情进展顺利,因为我关注了this question's answer,除了当我关注 TextInput 小部件并取消焦点时,不再调用转义键快捷方式,或者更具体地说,on_key_down 事件。
有谁知道怎么回事?任何帮助将不胜感激!
编辑:这是我的代码,缩短和简化:
test.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', 0)
class MainScreen(Screen):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _keyboard_closed(self):
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
print(keycode[1])
if keycode[1] == 'escape':
if App.get_running_app().root.current == 'Main':
App.get_running_app().stop()
Window.close()
else:
App.get_running_app().root.current = 'Main'
class UhIDontHaveIdeaForANameBecauseItsAnExampleScreen(Screen):
pass
class Test(App):
pass
Test().run()
test.kv
ScreenManager:
MainScreen:
id: Main
UhIDontHaveIdeaForANameBecauseItsAnExampleScreen:
id: Settings
<MainScreen>:
name: 'Main'
BoxLayout:
orientation: 'vertical'
Label:
text: 'idk just press it bruh'
Button:
id: butt
text: 'no clue'
on_release: app.root.current = 'NoClue'
<UhIDontHaveIdeaForANameBecauseItsAnExampleScreen>:
name: 'NoClue'
TextInput:
size_hint: 0.4, 0.1
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
【问题讨论】:
-
你能分享你的代码并描述你尝试了什么以及你想要什么
-
我用简化代码编辑了我的问题,你可以在那里查看!