bugfix> python > 投稿

私は python-2.7 を使用していますおよび kivy test.py を実行するときその後、 focus を設定します button で その後、マウスを使用してウィンドウの任意の場所をクリックすると、フォーカスは削除されません。ウィンドウをクリックしてEnterキーを押すと、 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()

回答 1 件
  • on_touch_up を追加できます   User のメソッド  クラス。

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

あなたの答え