为什么将 TextInput 焦点设置为 True 会使 TextInput 不响应键盘输入?
Why is it that setting TextInput focus equal to True, makes TextInput not respond to keyboard input?
背景:
我有一个按钮,可以将 TextInput
小部件添加到带有 focus: True
的 GridLayout
。如果 TextInput
已经存在于带有 focus = True
的布局中,则先前的 TextInput
首先未聚焦,然后添加新的 TextInput
focus = True
.
问题:
添加 TextInput
后,即使光标聚焦在添加的 TextInput
上,我也无法输入。
次要问题是当用户单击另一个小部件时 TextInput
保持焦点。这意味着无法触发 unfocus 事件。
尝试过:
我删除了显式设置焦点,但是添加 TextInput
后开始输入所需的结果丢失了。
次要我尝试设置 unfocus_on_touch = True
,但即使在用户离开 TextInput
.
后 TextInput
仍保持专注
代码:
import kivy
kivy.require("1.10.1")
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.uix.bubble import Bubble
from kivy.lang import Builder
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: ''
focus: True
foreground_color: (0,0.61,0.36,1) if self.focus else (0.71,0.75,0.71,1)
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:
text: 'Set'
color: (0,0,0,1)
pos_hint: {'top': 1, 'right': 0.9}
size_hint: [0.1, None]
height: Window.height / 15
Button:
text: '+ Plus'
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()
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 Notation(FloatLayout):
which_txt = ObjectProperty(None)
new_txt = ObjectProperty(None)
def add_input(self):
txt_hld = self.ids.text_holder
if self.new_txt == None:
self.new_txt = TextInput()
txt_hld.add_widget(self.new_txt)
else:
self.new_txt.focus = False
print(self.new_txt.focus)
self.new_txt = TextInput(focus = True)
print(self.new_txt.focus)
txt_hld.add_widget(self.new_txt)
def que_txt_input(self, instance):
self.which_txt = instance
print(instance)
def del_txt_input(self):
print(self.which_txt)
class theNoteApp(App):
title = 'My Notes'
def build(self):
return Notation()
if __name__ == '__main__':
theNoteApp().run()
想要的结果:
我想在添加新的 TextInput
后开始输入,在单击另一个小部件后我希望 TextInput
失去焦点并触发 unfocus_on_touch 事件?
前段时间我遇到了同样的问题,我在kivy的github页面打开了一个issue。
目前还没有解决方案,但有一个解决方法。
您必须将这些行添加到您的 TextInput
规则中:
keyboard_mode: 'managed'
keyboard_on_key_down: self.show_keyboard()
在那之后,它的工作,但随着你添加 widgets
的方式,你始终关注所有这些,所以你必须独立地解决这个问题。
您唯一需要关注的是您的 TextInput
规则,别无他处..
背景:
我有一个按钮,可以将 TextInput
小部件添加到带有 focus: True
的 GridLayout
。如果 TextInput
已经存在于带有 focus = True
的布局中,则先前的 TextInput
首先未聚焦,然后添加新的 TextInput
focus = True
.
问题:
添加 TextInput
后,即使光标聚焦在添加的 TextInput
上,我也无法输入。
次要问题是当用户单击另一个小部件时 TextInput
保持焦点。这意味着无法触发 unfocus 事件。
尝试过:
我删除了显式设置焦点,但是添加 TextInput
后开始输入所需的结果丢失了。
次要我尝试设置 unfocus_on_touch = True
,但即使在用户离开 TextInput
.
TextInput
仍保持专注
代码:
import kivy
kivy.require("1.10.1")
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.uix.bubble import Bubble
from kivy.lang import Builder
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: ''
focus: True
foreground_color: (0,0.61,0.36,1) if self.focus else (0.71,0.75,0.71,1)
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:
text: 'Set'
color: (0,0,0,1)
pos_hint: {'top': 1, 'right': 0.9}
size_hint: [0.1, None]
height: Window.height / 15
Button:
text: '+ Plus'
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()
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 Notation(FloatLayout):
which_txt = ObjectProperty(None)
new_txt = ObjectProperty(None)
def add_input(self):
txt_hld = self.ids.text_holder
if self.new_txt == None:
self.new_txt = TextInput()
txt_hld.add_widget(self.new_txt)
else:
self.new_txt.focus = False
print(self.new_txt.focus)
self.new_txt = TextInput(focus = True)
print(self.new_txt.focus)
txt_hld.add_widget(self.new_txt)
def que_txt_input(self, instance):
self.which_txt = instance
print(instance)
def del_txt_input(self):
print(self.which_txt)
class theNoteApp(App):
title = 'My Notes'
def build(self):
return Notation()
if __name__ == '__main__':
theNoteApp().run()
想要的结果:
我想在添加新的 TextInput
后开始输入,在单击另一个小部件后我希望 TextInput
失去焦点并触发 unfocus_on_touch 事件?
前段时间我遇到了同样的问题,我在kivy的github页面打开了一个issue。
目前还没有解决方案,但有一个解决方法。
您必须将这些行添加到您的 TextInput
规则中:
keyboard_mode: 'managed'
keyboard_on_key_down: self.show_keyboard()
在那之后,它的工作,但随着你添加 widgets
的方式,你始终关注所有这些,所以你必须独立地解决这个问题。
您唯一需要关注的是您的 TextInput
规则,别无他处..