我想制作文本输入选择器(以便光标在其上闪烁),而不是单击文本输入本身而是单击按钮。怎么这么容易?
的.py:
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class Main(Screen):
def __init__(self, **kwargs):
super(Main, self).__init__(**kwargs)
def select_textinput(self, instance):
pass
class GUI(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Main())
return sm
if __name__ == '__main__':
GUI().run()
.kv:
#: kivy 1.10.0
<Main>:
name: "main_screen"
TextInput:
id: ti
text: "Try to put the cursor here without clicking"
Button:
text: "Click here to select the text input !"
on_press: root.select_textinput(ti)
答案 0 :(得分:0)
只需将焦点设置为True即可。但要保持它闪烁,你必须安排这个功能:
...
from kivy.clock import Clock
from functools import partial
class Main(Screen):
def __init__(self, **kwargs):
super(Main, self).__init__(**kwargs)
def select_textinput(self, instance):
Clock.schedule_once(partial(self.keep_blinking, instance), .5)
def keep_blinking(self, instance, *args):
instance.focus = True
...
答案 1 :(得分:0)
解决方案如下。有关详细信息,请参阅代码段,示例和输出。
cursor = (0, 0)
设置文本开头的当前光标位置focus = True
将焦点传递给TextInput。focus: True
,cursor_width: 8
)关注TextInput时取消选择。如果你需要在TextInput聚焦时显示选择,你应该延迟(使用Clock.schedule)调用函数来选择文本(select_all,select_text)。
def select_textinput(self, instance):
print("Button Clicked - Select Text")
instance.cursor = (0, 0)
instance.focus = True
Clock.schedule_once(lambda dt: self.select_text(instance, dt=dt), 0.1)
def select_text(self, instance, dt):
instance.select_all()
Button:
text: "Click here to select the text input !"
on_release: root.select_textinput(ti)
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
class Main(Screen):
def select_textinput(self, instance):
print("Button Clicked - Select Text")
instance.cursor = (0, 0)
instance.focus = True
Clock.schedule_once(lambda dt: instance.select_all())
class GUI(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Main())
return sm
if __name__ == '__main__':
GUI().run()
#: kivy 1.10.0
<Main>:
name: "main_screen"
BoxLayout:
orientation: 'vertical'
TextInput:
id: ti
focus: True
cursor_width: 8 # Default: 1sp
text: "Try to put the cursor here without clicking"
Button:
text: "Click here to select the text input !"
on_release: root.select_textinput(ti)