Python:如何在单击窗口上的任何位置时从按钮中删除焦点

时间:2018-05-25 05:30:51

标签: python python-2.7 kivy-language

我正在使用python-2.7kivy。当我运行test.py时,我在focus上设置button。然后我点击窗口上的任意位置使用鼠标然后焦点不会删除。因为点击窗口后我按回车键然后拨打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 个答案:

答案 0 :(得分:1)

您可以在File f = new File("suspiciousLockedFile.txt"); System.out.println(f.canWrite()); 课程中添加on_touch_up方法。

User

我正在发布完整的代码。

test.py

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.kv

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