Skip to main content
 首页 » 编程设计

python : How to remove focus from button when click anywhere on window

2024年12月31日9yjmyzz

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

测试.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() 

测试.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() 

请您参考如下方法:

您可以在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] 

我正在发布完整的代码。

测试.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() 

测试.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()