kivy 中的函数聚焦错误的输入
我在创建一种使用箭头移动到下一个 TextInput 的机制时遇到问题。该代码有效,但它没有将我放入正确的容器中。当我点击右、左、右时,我应该在第二个容器中,但我在 id3 的容器中。我发现应该更改 self.focused 属性的函数有时不起作用,但我不知道为什么以及如何修复它。
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
Builder.load_file("keyboardreader.kv")
class MyTextInput(TextInput):
focused=StringProperty('id1')
def change_focus(self, *args):
app = App.get_running_app()
if app.root is not None:
# Now access the container.
layout = app.root.ids["layout"]
# Access the required widget and set its focus.
print("Changefocus",self.focused)
layout.ids[self.focused].focus = True
def keyboard_on_key_down(self, window, keycode, text, modifiers):
print(keycode, text, modifiers)
focusedid=int(self.focused[2])
if keycode[1]=="backspace":
self.text=self.text[:-1]
if keycode[1]=="right":
if int(self.focused[2])<5:
focusedid+=1
self.focused="id"+str(focusedid)
elif keycode[1]=="left":
if int(self.focused[2])>1:
self.text=""
focusedid-=1
self.focused="id"+str(focusedid)
self.change_focus()
print("After changing",self.focused)
return True
#TextInput.keyboard_on_key_down(self, window, keycode, text, modifiers)
class MainScreen(Widget):
pass
class TestingappApp(App):
def build(self):
return MainScreen()
TestingappApp().run()
<MainScreen>:
CustomBox:
id: layout
size: root.size
<CustomBox@BoxLayout>:
MyTextInput:
id: id1
focused: "id1"
MyTextInput:
id: id2
focused: "id2"
MyTextInput:
id: id3
focused: "id3"
MyTextInput:
id: id4
focused: "id4"
MyTextInput:
id: id5
focused: "id5"
I'm having a trouble creating a mechanism moving to the next TextInput with arrows. This code works, but it doesn't put me in the right container. While i click right,left,right I should be at 2nd container, but I am at the one with id3. I figured out that the function which is supposed to change the self.focused attribute sometimes doesn't work, but I don't know why and how to fix it.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
Builder.load_file("keyboardreader.kv")
class MyTextInput(TextInput):
focused=StringProperty('id1')
def change_focus(self, *args):
app = App.get_running_app()
if app.root is not None:
# Now access the container.
layout = app.root.ids["layout"]
# Access the required widget and set its focus.
print("Changefocus",self.focused)
layout.ids[self.focused].focus = True
def keyboard_on_key_down(self, window, keycode, text, modifiers):
print(keycode, text, modifiers)
focusedid=int(self.focused[2])
if keycode[1]=="backspace":
self.text=self.text[:-1]
if keycode[1]=="right":
if int(self.focused[2])<5:
focusedid+=1
self.focused="id"+str(focusedid)
elif keycode[1]=="left":
if int(self.focused[2])>1:
self.text=""
focusedid-=1
self.focused="id"+str(focusedid)
self.change_focus()
print("After changing",self.focused)
return True
#TextInput.keyboard_on_key_down(self, window, keycode, text, modifiers)
class MainScreen(Widget):
pass
class TestingappApp(App):
def build(self):
return MainScreen()
TestingappApp().run()
<MainScreen>:
CustomBox:
id: layout
size: root.size
<CustomBox@BoxLayout>:
MyTextInput:
id: id1
focused: "id1"
MyTextInput:
id: id2
focused: "id2"
MyTextInput:
id: id3
focused: "id3"
MyTextInput:
id: id4
focused: "id4"
MyTextInput:
id: id5
focused: "id5"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您将
focused
创建为StringProperty
,这意味着MyTextInput
的每个实例都将拥有自己的focused 实例
,这样对一个的更改就不会被其他人知道。解决方法是使focused
成为一个类级别变量,并让代码中的所有引用都引用该一个类级别变量。像这样:The problem is that you are creating
focused
as aStringProperty
, which means that each instance ofMyTextInput
will have its own instance offocused
, so that changes to one will not be known by the others. The fix is to makefocused
a class level variable, and have all the references in your code refer to that one class level variable. Like this: