kivy 中的函数聚焦错误的输入

发布于 2025-01-13 00:43:32 字数 2146 浏览 0 评论 0原文

我在创建一种使用箭头移动到下一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久伴你 2025-01-20 00:43:32

问题是您将 focused 创建为 StringProperty,这意味着 MyTextInput 的每个实例都将拥有自己的 focused 实例,这样对一个的更改就不会被其他人知道。解决方法是使 focused 成为一个类级别变量,并让代码中的所有引用都引用该一个类级别变量。像这样:

class MyTextInput(TextInput):
    focused = '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", MyTextInput.focused)
            layout.ids[MyTextInput.focused].focus = True

    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        focusedid = int(MyTextInput.focused[2])
        if keycode[1] == "backspace":
            self.text = self.text[:-1]
        if keycode[1] == "right":
            if int(MyTextInput.focused[2]) < 5:
                focusedid += 1
                MyTextInput.focused = "id" + str(focusedid)
        elif keycode[1] == "left":
            if int(MyTextInput.focused[2]) > 1:
                self.text = ""
                focusedid -= 1
                MyTextInput.focused = "id" + str(focusedid)
        self.change_focus()
        print("After changing", MyTextInput.focused)
        return True

The problem is that you are creating focused as a StringProperty, which means that each instance of MyTextInput will have its own instance of focused, so that changes to one will not be known by the others. The fix is to make focused a class level variable, and have all the references in your code refer to that one class level variable. Like this:

class MyTextInput(TextInput):
    focused = '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", MyTextInput.focused)
            layout.ids[MyTextInput.focused].focus = True

    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        focusedid = int(MyTextInput.focused[2])
        if keycode[1] == "backspace":
            self.text = self.text[:-1]
        if keycode[1] == "right":
            if int(MyTextInput.focused[2]) < 5:
                focusedid += 1
                MyTextInput.focused = "id" + str(focusedid)
        elif keycode[1] == "left":
            if int(MyTextInput.focused[2]) > 1:
                self.text = ""
                focusedid -= 1
                MyTextInput.focused = "id" + str(focusedid)
        self.change_focus()
        print("After changing", MyTextInput.focused)
        return True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文