使用 kivy 和 python 返回屏幕管理器时未设置焦点
Not set focus when come back screen manager with kivy and python
我想在文本输入中设置焦点。一开始焦点设置正确,但是当我转到下一个屏幕并返回初始屏幕时,焦点设置不正确。
这是一个带有 rfid 读取器的应用程序,我想读取代码并 select 进入或退出
main.py
import kivy
kivy.require('1.10.0') # replace with your current kivy version !
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
class MyScreenManager(ScreenManager):
def __init__(self,**kwargs):
super(MyScreenManager,self).__init__()
class Menu1(Screen):
def __init__(self, **kwargs):
super(Menu1, self).__init__()
class Menu2(Screen):
def __init__(self, **kwargs):
super(Menu2, self).__init__()
Builder.load_file("main.kv")
class Fichaje(App):
def build(self):
return MyScreenManager()
if __name__ == '__main__':
Fichaje().run()
main.kv
#:kivy 1.10.0
#:import WipeTransition kivy.uix.screenmanager.WipeTransition
<MyScreenManager>:
#transition: WipeTransition()
Menu1:
id: menu1
Menu2:
id: menu2
<Menu1>:
name: "screen1"
BoxLayout:
orientation: 'vertical'
TextInput:
id: input1
size_hint_y: .1
multiline: False
focus: True
on_text_validate:
root.manager.current = "screen2"
BoxLayout:
<Menu2>:
name: "screen2"
BoxLayout:
Button:
text:"Entrada"
on_press:
root.manager.current = "screen1"
Button:
text:"Salida"
on_press:
root.manager.current = "screen1"
没有错误消息,但焦点不在正确的站点上,
谢谢
我更改代码以简化错误
在示例中,没有尝试更改焦点。但我认为这已经尝试过了,但它又失去了焦点。
文本输入再次失去焦点的原因是它在释放鼠标或点击之前获得焦点。 on_press
方法后跟 on_release
,其中文本输入再次失去焦点。
要解决此问题,您只需在 on_release
方法中设置焦点即可。
最快的就是在kv文件中只加一行代码,把on_press
改成on_release
。
root.manager.get_screen("screen1").ids["input1"].focus
例如,可以通过在屏幕 1 中使用对象 属性 来改变此行。或者如果你不能使用on_release
方法,也许使用时钟安排一个焦点在一段时间内,如果触摸仍然按下,重新安排它。
但这是快速修复。
<Menu2>:
name: "screen2"
BoxLayout:
Button:
text:"Entrada"
on_release:
root.manager.current = "screen1"
root.manager.get_screen("screen1").ids["input1"].focus = True
我想在文本输入中设置焦点。一开始焦点设置正确,但是当我转到下一个屏幕并返回初始屏幕时,焦点设置不正确。
这是一个带有 rfid 读取器的应用程序,我想读取代码并 select 进入或退出
main.py
import kivy
kivy.require('1.10.0') # replace with your current kivy version !
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
class MyScreenManager(ScreenManager):
def __init__(self,**kwargs):
super(MyScreenManager,self).__init__()
class Menu1(Screen):
def __init__(self, **kwargs):
super(Menu1, self).__init__()
class Menu2(Screen):
def __init__(self, **kwargs):
super(Menu2, self).__init__()
Builder.load_file("main.kv")
class Fichaje(App):
def build(self):
return MyScreenManager()
if __name__ == '__main__':
Fichaje().run()
main.kv
#:kivy 1.10.0
#:import WipeTransition kivy.uix.screenmanager.WipeTransition
<MyScreenManager>:
#transition: WipeTransition()
Menu1:
id: menu1
Menu2:
id: menu2
<Menu1>:
name: "screen1"
BoxLayout:
orientation: 'vertical'
TextInput:
id: input1
size_hint_y: .1
multiline: False
focus: True
on_text_validate:
root.manager.current = "screen2"
BoxLayout:
<Menu2>:
name: "screen2"
BoxLayout:
Button:
text:"Entrada"
on_press:
root.manager.current = "screen1"
Button:
text:"Salida"
on_press:
root.manager.current = "screen1"
没有错误消息,但焦点不在正确的站点上, 谢谢
我更改代码以简化错误
在示例中,没有尝试更改焦点。但我认为这已经尝试过了,但它又失去了焦点。
文本输入再次失去焦点的原因是它在释放鼠标或点击之前获得焦点。 on_press
方法后跟 on_release
,其中文本输入再次失去焦点。
要解决此问题,您只需在 on_release
方法中设置焦点即可。
最快的就是在kv文件中只加一行代码,把on_press
改成on_release
。
root.manager.get_screen("screen1").ids["input1"].focus
例如,可以通过在屏幕 1 中使用对象 属性 来改变此行。或者如果你不能使用on_release
方法,也许使用时钟安排一个焦点在一段时间内,如果触摸仍然按下,重新安排它。
但这是快速修复。
<Menu2>:
name: "screen2"
BoxLayout:
Button:
text:"Entrada"
on_release:
root.manager.current = "screen1"
root.manager.get_screen("screen1").ids["input1"].focus = True