【问题标题】:Python/Kivy : focus not set when use mousePython/Kivy:使用鼠标时未设置焦点
【发布时间】:2018-08-25 12:50:38
【问题描述】:

我正在使用python-2.7kivy
如果我使用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

但是当我使用鼠标调用此函数时,它并没有聚焦到TextInput。有人帮我知道当我使用mouse 单击ok 按钮时如何将焦点设置到TextInput

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()

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    您必须使用on_release,因为一旦按下按钮就会调用on_press,稍后鼠标会重新获得焦点,因为它正在按下按钮,另一方面,当它没有按下任何按钮时将获得焦点。

    Button:
        id:test3
        text: 'Ok'
        focus: False
        on_release : root.abc()
    

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多