Python/Kivy : 使用鼠标时未设置焦点
Python/Kivy : focus not set when use mouse
我正在使用 python-2.7
和 kivy
。
如果我使用 mouse
单击 ok
按钮,则函数调用 on_press : root.abc()
.
def abc(self):
if self.test1.text.strip() == "":
self.test1.focus = True
return False
在这个函数中,我检查 test1
textInput 是否为空白然后光标聚焦到空白 TextInput
。
self.test1.focus = True
但是当我使用 mouse.Someone 调用这个函数时它没有聚焦到 TextInput
帮助我当我点击 ok
按钮时如何将焦点设置到 TextInput
使用 mouse
?
test.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.properties import ObjectProperty
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (300, 100)
class User(Screen):
test3 = ObjectProperty(None)
def __init__(self, **kwargs):
super(User, self).__init__(**kwargs)
Window.bind(on_key_down=self._on_keyboard_down)
def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
if self.test3.focus and keycode == 40: # 40 - Enter key pressed
self.test3.focus = False
self.abc()
return True
def abc(self):
if self.test1.text.strip() == "":
self.test1.focus = True
return False
class Test(App):
def build(self):
return self.root
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
User:
test1 : test1
test3: test3
BoxLayout:
orientation: "vertical"
TextInput:
id:test1
focus : True
text: ' '
width: 100
multiline: False
on_text_validate: test2.focus = True
TextInput:
id:test2
text: ' '
width: 100
multiline: False
on_text_validate:
test3.background_normal = ''
test3.background_color = [0, 0, 1, 0.5] # 50% translucent blue
test3.focus = True
Button:
id:test3
text: 'Ok'
focus: False
on_press : root.abc()
你必须使用 on_release
因为一旦按下按钮就会调用 on_press
按完就没人要焦点了。
Button:
id:test3
text: 'Ok'
focus: False
on_release : root.abc()
我正在使用 python-2.7
和 kivy
。
如果我使用 mouse
单击 ok
按钮,则函数调用 on_press : root.abc()
.
def abc(self):
if self.test1.text.strip() == "":
self.test1.focus = True
return False
在这个函数中,我检查 test1
textInput 是否为空白然后光标聚焦到空白 TextInput
。
self.test1.focus = True
但是当我使用 mouse.Someone 调用这个函数时它没有聚焦到 TextInput
帮助我当我点击 ok
按钮时如何将焦点设置到 TextInput
使用 mouse
?
test.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.properties import ObjectProperty
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (300, 100)
class User(Screen):
test3 = ObjectProperty(None)
def __init__(self, **kwargs):
super(User, self).__init__(**kwargs)
Window.bind(on_key_down=self._on_keyboard_down)
def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
if self.test3.focus and keycode == 40: # 40 - Enter key pressed
self.test3.focus = False
self.abc()
return True
def abc(self):
if self.test1.text.strip() == "":
self.test1.focus = True
return False
class Test(App):
def build(self):
return self.root
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
User:
test1 : test1
test3: test3
BoxLayout:
orientation: "vertical"
TextInput:
id:test1
focus : True
text: ' '
width: 100
multiline: False
on_text_validate: test2.focus = True
TextInput:
id:test2
text: ' '
width: 100
multiline: False
on_text_validate:
test3.background_normal = ''
test3.background_color = [0, 0, 1, 0.5] # 50% translucent blue
test3.focus = True
Button:
id:test3
text: 'Ok'
focus: False
on_press : root.abc()
你必须使用 on_release
因为一旦按下按钮就会调用 on_press
按完就没人要焦点了。
Button:
id:test3
text: 'Ok'
focus: False
on_release : root.abc()