Python kivy TextInput是否有焦点丢失事件调度器? 问题:

Python kivy TextInput是否有焦点丢失事件调度器? 问题:,python,python-3.x,kivy,kivy-language,Python,Python 3.x,Kivy,Kivy Language,如果multiline=True TextInput失去焦点,是否有方法触发事件 背景: 我尝试了触摸功能的。但是它返回所有我的文本输入的实例,而不仅仅是当前小部件。我尝试了text\u validate\u unfocus=False,结果相同 代码: 导入kivy kivy.require(“1.10.1”) 从kivy.app导入应用程序 从kivy.uix.floatlayout导入floatlayout 从kivy.properties导入ObjectProperty、NumericP

如果
multiline=True TextInput
失去焦点,是否有方法触发事件

背景: 我尝试了触摸功能的
。但是它返回所有我的
文本输入的实例,而不仅仅是当前小部件。我尝试了
text\u validate\u unfocus=False
,结果相同

代码:
导入kivy
kivy.require(“1.10.1”)
从kivy.app导入应用程序
从kivy.uix.floatlayout导入floatlayout
从kivy.properties导入ObjectProperty、NumericProperty
从kivy.uix.textinput导入textinput
从kivy.uix.bubble导入bubble
从kivy.lang导入生成器
从kivy.storage.jsonstore导入jsonstore
Builder.load_字符串(“”)
#:导入窗口kivy.core.Window.Window
:
背景\u正常:“”
:
在以下情况之前:
颜色:
rgba:(0,0.59,0.36,1)
矩形:
pos:self.pos
大小:self.size
:
提示文字:“Nuwe nota”
字体大小:self.height/4.5如果self.focus或self.height/3
背景\u正常:“”
背景\u处于活动状态:“”
前景颜色:(0,0.61,0.36,1)如果self.focus-else(0.71,0.75,0.71,1)
未聚焦于触摸:错误
在下列情况之后:
颜色:
rgb:(0,0,0,1)
行:
积分:self.pos[0]、self.pos[1]、self.pos[0]+self.size[0]、self.pos[1]
尺寸提示:无
高度:Window.height/6如果是self.focus else Window.height/12
:
方向:“水平”
大小提示:(无,无)
尺寸:(160120)
位置提示:{'top':0.2,'right':0.8}
箭头位置:“左上方”
泡泡按钮:
文本:“保存”
泡泡按钮:
文本:“加密…”
泡泡按钮:
文本:“删除”
发布时:root.del_txt_input()
:
画布:
颜色:
rgba:(0,0.43,0.37,1)
矩形:
pos:self.pos
大小:self.size
标签:
位置提示:{'top':1,'right':0.8}
尺寸提示:[0.8,无]
高度:窗高/15
按钮:
颜色:(0,0,0,1)
位置提示:{'top':1,'right':0.9}
大小提示:[0.1,无]
高度:窗高/15
图片:
资料来源:“gear_2.png”
中心y:self.parent.center
center\u x:self.parent.center\u x
大小:self.parent.width/1.5,self.parent.height/1.5
允许拉伸:真
按钮:
颜色:(0,0,0,1)
位置提示:{'top':1,'right':1}
大小提示:[0.1,无]
高度:窗高/15
发布时:root.add\u input()
图片:
来源:“plus_text12354.png”
中心y:self.parent.center
center\u x:self.parent.center\u x
大小:self.parent.width/1.5,self.parent.height/1.5
允许拉伸:真
滚动视图:
尺寸提示:无
尺寸:窗宽、窗高
pos_提示:{'top':0.92,'right':1}
网格布局:
id:文本保存器
科尔斯:1
pos_提示:{'top':0.92,'right':1}
填充:4
大小提示:1
尺寸提示:无
高度:自身最小高度
''')
类别选择气泡(气泡):
通过
类TextInput(TextInput):
got_txt=ObjectProperty(无)
def on_touch_up(自我,触摸):
如果不是自碰撞点(*touch.pos):
self.text\u validate\u unfocus=False
注=符号()
注:显示气泡
self.got_txt=note.que_txt_输入(self)
返回超级(文本输入,自我)。打开触摸(触摸)
类表示法(浮动布局):
哪个_txt=ObjectProperty(无)
new_txt=ObjectProperty(无)
cnt=NumericProperty(0)
定义初始(自我,**kwargs):
超级()
self.the_file=JsonStore('txt_input.json'))
self.cnt=self.the_file.count()
lst=self.the_file.keys
def添加_输入(自身):
txt_hld=self.ids.text_
self.cnt+=1
self.new_txt=TextInput(id=str(self.cnt))
self.the_file.put(str(self.cnt),the_id=str(self.cnt),the_input='')
txt\u hld.add\u小部件(self.new\u txt)
def que_txt_输入(自身,实例):
self.which_txt=实例
打印(instance.text,instance)
返回实例
def del_txt_输入(自身):
打印(self.which_txt)
定义_文件(self,notestore):
self.notestore=notestore
def显示气泡(自身):
self.add_小部件(ChoiceBubble())
def get_存储(自):
\u keys=list(self.the\u file.keys)
打印(_键)
把钥匙还给我
类应用程序(应用程序):
标题=‘我的笔记’
def生成(自):
返回符号()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
theNoteApp().run()的
预期结果:
一旦焦点丢失,我希望将一个气泡
小部件
添加到我的
根类
的顶部。这将使用户可以选择将
TextInput
id和刚刚失去焦点的文本保存、加密或删除到
JSON
文件中。

当焦点布尔值更改时,焦点上的
事件将触发

我已经试过了润色功能。但它返回所有文本输入的实例,而不仅仅是当前的小部件

这是因为您没有编写任何代码将其限制在您关心的小部件上,如果您愿意,可以这样做。

  • 对象的多个实例,
    表示法
    。一个来自
    App
    类中的
    build()
    方法(
    return Notation()
    ),另一个实例是在
    on\u touch\u()
    方法(
    note=Notation()
    )中创建的,只要触发
    on\u touch\u
    事件。在
    on\u touch\u up()
    方法中创建的那些实例没有可见的视图,即它不会显示在窗口中
  • AttributeError:“ChoiceBubble”对象没有属性“del\u txt\u input”
  • ChoiceBubble
    中的文本不可见。
    import kivy
    kivy.require("1.10.1")
    
    from kivy.app import App
    from kivy.uix.floatlayout import FloatLayout
    from kivy.properties import ObjectProperty, NumericProperty
    from kivy.uix.textinput import TextInput
    from kivy.uix.bubble import Bubble
    from kivy.lang import Builder
    from kivy.storage.jsonstore import JsonStore
    
    Builder.load_string('''
    #: import Window kivy.core.window.Window
    <Button>:
        background_normal: ''
    <Label>:
        canvas.before:
            Color:
                rgba: (0,0.59,0.36,1)
            Rectangle:
                pos: self.pos
                size: self.size
    <TextInput>:
        hint_text: 'Nuwe nota'
        font_size: self.height / 4.5 if self.focus else self.height / 3
        background_normal: ''
        background_active: ''
        foreground_color: (0,0.61,0.36,1) if self.focus else (0.71,0.75,0.71,1)
        unfocus_on_touch: False
        canvas.after:
            Color:
                rgb: (0,0,0,1)
            Line:
                points: self.pos[0] , self.pos[1], self.pos[0] + self.size[0], self.pos[1]
        size_hint_y: None
        height: Window.height / 6 if self.focus else Window.height / 12
    <ChoiceBubble>:
        orientation: 'horizontal'
        size_hint: (None, None)
        size: (160, 120)
        pos_hint: {'top': 0.2, 'right': 0.8}
        arrow_pos: 'top_left'
        BubbleButton:
            text: 'Save'
        BubbleButton:
            text: 'Encrypt..'
        BubbleButton:
            text: 'Delete'
            on_release: root.del_txt_input()
    <Notation>:
        canvas:
            Color:
                rgba: (0,0.43,0.37,1)
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            pos_hint: {'top': 1, 'right': 0.8}
            size_hint: [0.8, None]
            height: Window.height / 15
        Button:
            color: (0,0,0,1)
            pos_hint: {'top': 1, 'right': 0.9}
            size_hint: [0.1, None]
            height: Window.height / 15
            Image:
                source: 'gear_2.png'
                center_y: self.parent.center_y
                center_x: self.parent.center_x
                size: self.parent.width /1.5, self.parent.height/ 1.5
                allow_stretch: True
        Button:
            color: (0,0,0,1)
            pos_hint: {'top': 1, 'right': 1}
            size_hint: [0.1, None]
            height: Window.height / 15
            on_release: root.add_input()
            Image:
                source: 'plus_text12354.png'
                center_y: self.parent.center_y
                center_x: self.parent.center_x
                size: self.parent.width /1.5, self.parent.height/ 1.5
                allow_stretch: True
        ScrollView:
            size_hint_y: None
            size: Window.width, Window.height
            pos_hint: {'top': 0.92, 'right': 1}
            GridLayout:
                id: text_holder
                cols: 1
                pos_hint: {'top': 0.92, 'right': 1}
                padding: 4
                size_hint_x: 1
                size_hint_y: None
                height: self.minimum_height
    
    ''')
    class ChoiceBubble(Bubble):
        pass
    class TextInput(TextInput):
        got_txt = ObjectProperty(None)
        def on_touch_up(self, touch):
            if not self.collide_point(*touch.pos):
                self.text_validate_unfocus = False
                note = Notation()
                note.show_bubble
                self.got_txt=note.que_txt_input(self)
            return super(TextInput, self).on_touch_up(touch)
    class Notation(FloatLayout):
        which_txt = ObjectProperty(None)
        new_txt = ObjectProperty(None)
        cnt = NumericProperty(0)
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.the_file=JsonStore('txt_input.json')
            self.cnt = self.the_file.count()
            lst = self.the_file.keys
        def add_input(self):
            txt_hld = self.ids.text_holder
            self.cnt += 1
            self.new_txt = TextInput(id=str(self.cnt))
            self.the_file.put(str(self.cnt), the_id=str(self.cnt), the_input='')
            txt_hld.add_widget(self.new_txt)
        def que_txt_input(self, instance):
            self.which_txt = instance
            print(instance.text, instance)
            return instance
        def del_txt_input(self):
            print(self.which_txt)
        def the_file(self, notestore):
            self.notestore = notestore
        def show_bubble(self):
            self.add_widget(ChoiceBubble())
        def get_store(self):
            the_keys = list(self.the_file.keys)
            print(the_keys)
            return the_keys
    
    class theNoteApp(App):
        title = 'My Notes'
        def build(self):
            return Notation()
    
    if __name__ == '__main__':
        theNoteApp().run()
    
    
    <Label>:
        color: 0, 0, 0, 1
        ...
    
    <ChoiceBubble>:
        ...
        BubbleButton:
            text: 'Delete'
            on_release: app.root.del_txt_input()
    
    class TextInput(TextInput):
        got_txt = ObjectProperty(None)
    
        def on_focus(self, instance, value):
            if not value:   # defocused
                note = App.get_running_app().root
                note.show_bubble()
                self.got_txt = note.que_txt_input(self)