Python 基维选择焦点
我试图让kivy在焦点上选择TextInput小部件的文本,但当我尝试时,它似乎在取消焦点并保留选择时选择它。你知道我如何选择焦点和取消焦点吗?如果有人想玩,我会在下面附上我的代码 kv文件:Python 基维选择焦点,python,kivy,kivy-language,Python,Kivy,Kivy Language,我试图让kivy在焦点上选择TextInput小部件的文本,但当我尝试时,它似乎在取消焦点并保留选择时选择它。你知道我如何选择焦点和取消焦点吗?如果有人想玩,我会在下面附上我的代码 kv文件: <TextInput>: size_hint: 0.9, 0.5 pos_hint: {'center_x': 0.5, 'center_y': 0.5} multiline: False <Button>: text: "Press Me"
<TextInput>:
size_hint: 0.9, 0.5
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
multiline: False
<Button>:
text: "Press Me"
size_hint: (0.1, 0.5)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
<MainLayout>:
canvas.before:
Color:
rgba: 0.15, 0.15, 0.16, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
padding: 10
BoxLayout:
padding: 10
TextInput:
text: "Directory"
Button:
text: "Browse"
on_press: root.browse_btn()
BoxLayout:
padding: 10
TextInput:
text: "Prefix"
on_focus: self.select_all()
TextInput:
text: "File"
on_focus: self.select_all()
TextInput:
text: "Suffix"
on_focus: self.select_all()
BoxLayout:
padding: 10
Button:
id: button_one
text: "Confirm"
on_press: root.confirm_btn()
Button:
text: "Cancel"
on_press: root.cancel_btn()
隐藏在
TextInput
文档中:
当文本输入被聚焦时,选择被取消。如果你需要展示
选择当文本输入聚焦时,您应该延迟(使用
Clock.schedule)调用用于选择文本的函数
(选择全部,选择文本)
因此,在您的kv
中,从导入时钟开始:
#: import Clock kivy.clock.Clock
然后您可以在TextInput
规则中使用它:
TextInput:
text: "Prefix"
on_focus: Clock.schedule_once(lambda dt: self.select_all()) if self.focus else None
if self.focus
确保select\u all
仅在TextInput
获得焦点时发生。是的,如何管理它不是很明显。起初我确实有时钟功能,但它也在做同样的事情。我绝对没有想到添加if语句,但这解决了我的问题!谢谢
TextInput:
text: "Prefix"
on_focus: Clock.schedule_once(lambda dt: self.select_all()) if self.focus else None