gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:41:22 25 4
gpt4 key购买 nike

我正在使用 python-2.7kivy 。当我运行 test.py 然后我设置 focusbutton 上。之后我使用鼠标单击窗口上的任意位置,然后焦点不会移除。因为在点击窗口后我按下 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()

关于 python : How to remove focus from button when click anywhere on window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50522099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com