【问题标题】:Python : How to remove focus from button when click anywhere on windowPython:单击窗口上的任意位置时如何从按钮上移除焦点
【发布时间】:2018-05-25 05:30:51
【问题描述】:

我正在使用python-2.7kivy。当我运行test.py 时,我在button 上设置focus。之后,我使用鼠标单击窗口上的任意位置,然后焦点不会移除。因为点击窗口后我按回车然后它调用def self.add().
有人可以告诉我如何在单击任意位置时从按钮中删除 focus
窗户?

test.py

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import Clock

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 150)


class User(Screen):
    name = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)
        Clock.schedule_once(self.name_focus, 1)

    def name_focus(self, *args):
        self.postUser.focus = True
        self.postUser.background_color = [0.5, 0.5, 0.5, 1]


    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if (hasattr(self.postUser, 'focus') and self.postUser.focus) and keycode == 40:
            self.add()

    def add(self):
        print('button Event Call')


class Test(App):

    def build(self):
        return self.root


if __name__ == '__main__':
    Test().run()

test.kv

#:kivy 1.10.0

User:
    name: name
    postUser : postUser
    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            padding: 20, 20
            spacing: 10, 10

            Label:
                text: "Name"
                text_size: self.size
                valign: 'middle'
            TextInput:
                id:name
                text_size: self.size
        GridLayout:
            cols: 2
            padding: 0, 0
            spacing: 5, 0
            size_hint: .5, .35
            pos_hint: {'x': .25, 'y': 0}

            Button:
                id:postUser
                size_hint_x: .5
                text: "Ok"
                focus: False
                on_release:
                    root.add()

【问题讨论】:

    标签: python python-2.7 kivy-language


    【解决方案1】:

    您可以在 User 类中添加 on_touch_up 方法。

    def on_touch_up(self, touch):
        if (hasattr(self.postUser, 'focus') and self.postUser.focus):
            self.postUser.focus = False
            self.postUser.background_color = [1, 1, 1, 1]
    

    我正在发布完整的代码。

    test.py

    from kivy.uix.screenmanager import Screen
    from kivy.app import App
    from kivy.core.window import Window
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import StringProperty, ObjectProperty
    from kivy.clock import Clock
    
    Window.clearcolor = (0.5, 0.5, 0.5, 1)
    Window.size = (500, 150)
    
    
    class User(Screen):
        name = ObjectProperty(None)
    
        def __init__(self, **kwargs):
            super(User, self).__init__(**kwargs)
            Window.bind(on_key_down=self._on_keyboard_down)
            Clock.schedule_once(self.name_focus, 1)
    
        def name_focus(self, *args):
            self.postUser.focus = True
            self.postUser.background_color = [0.5, 0.5, 0.5, 1]
    
    
        def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
            if (hasattr(self.postUser, 'focus') and self.postUser.focus) and keycode == 40:
                self.add()
    
        def add(self):
            print('button Event Call')
    
        def on_touch_up(self, touch):
            if (hasattr(self.postUser, 'focus') and self.postUser.focus):
                self.postUser.focus = False
                self.postUser.background_color = [1, 1, 1, 1]
    
    
    class Test(App):
    
        def build(self):
            return self.root
    
    
    if __name__ == '__main__':
        Test().run()
    

    test.kv

    #:kivy 1.10.0
    
    User:
        name: name
        postUser : postUser
        BoxLayout:
            orientation: "vertical"
            GridLayout:
                cols: 2
                padding: 20, 20
                spacing: 10, 10
    
                Label:
                    text: "Name"
                    text_size: self.size
                    valign: 'middle'
                TextInput:
                    id:name
                    text_size: self.size
            GridLayout:
                cols: 2
                padding: 0, 0
                spacing: 5, 0
                size_hint: .5, .35
                pos_hint: {'x': .25, 'y': 0}
    
                Button:
                    id:postUser
                    size_hint_x: .5
                    text: "Ok"
                    focus: False
                    on_release:
                        root.add()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 2011-08-14
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多