Python-Kivy-Builtdozer:让按钮调用android键盘 [英] Python - Kivy - Builtdozer : Having a button call the android keyboard
问题描述
框架:我说的是一个用Python 2.7编写并与Builtdozer打包在一起的Android应用
Framework: I am talking about an Android app written in Python 2.7 and packaged with Builtdozer
我的应用程序内部有一个带有按钮的构建器
I have a Builder inside the app with a button
Builder.load_string('''
FloatLayout:
orientation: 'horizontal'
Button:
text: str(root.name)
font_size: '20sp'
pos_hint: {'x':.0, 'y':.3}
size_hint: .4, .8
''')
我想创建一个函数change_name
,如果我按上面的按钮,则会打开android键盘以接受用户raw_input
I want to create a function, change_name
, that, if I press the above button, opens the android keyboard to accept an user raw_input
用户提供的raw_input
必须替换上面按钮的文本.
The raw_input
provided by the user has to replace the text of the above button.
我的想法是:
1)创建一个变量name = StringProperty('Me')
2)创建一个函数:
def change_name(self):
self.name = raw_input()
3)用on_release
Builder.load_string('''
FloatLayout:
orientation: 'horizontal'
Button:
text: str(root.name)
font_size: '20sp'
pos_hint: {'x':.0, 'y':.3}
size_hint: .4, .8
on_release: root.change_name()
''')
这是正确的吗?因为实际上是在Ubuntu上运行该应用程序,所以我试图单击该按钮,但该应用程序不要求输入(它似乎已被阻止).
It is correct? Because actually, running the app on Ubuntu, I am trying to click on the button but the app does not ask for an input (it seems blocked).
因此,我认为它在Android上也无法使用.
As a consequence I believe it will not work also on Android.
能否请您帮助我了解我在哪里错了?
Could you please help me understanding where am I wrong?
推荐答案
raw_input
允许从 stdin (终端)获取输入.在Android上,您将没有可用的终端.另外, raw_input
正在阻止,这将导致应用程序的主事件循环冻结,并导致应用程序停止响应.
raw_input
allows to get input from stdin (terminal). On Android you will not have the terminal available. In addition, raw_input
is blocking, this causes the main event loop of your app to be freeze and will cause your app to stop responding.
您不应使用raw_input
,而应使用Kivy自己的方法.
You shouldn't use raw_input
but Kivy's own methods.
另一方面,您想使按钮可编辑(就像它是TextInput
一样).您可以创建自己的自定义Button类,也可以使用
On the other hand, you want to make your button editable (as if it were a TextInput
). You can create your own custom Button class or use WindowBase.request_keyboard() to request the keyboard manually. However, you can do a little trick by hiding a TextInput
and use it to enter the text:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
kv_text= ('''
<MyWidget>:
FloatLayout:
orientation: 'horizontal'
Button:
text: 'Hello'
font_size: '20sp'
pos_hint: {'x':.0, 'y':.3}
size_hint: .4, .8
on_release: root.change_name(self)
Button:
text: 'World'
font_size: '20sp'
pos_hint: {'x':0.6, 'y':.3}
size_hint: .4, 0.8
on_release: root.change_name(self)
''')
class MyWidget(FloatLayout):
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
self.hide_input = TextInput(size_hint=(None, None),
size = (0, 0),
multiline = False)
self.hide_input_bind = None
def change_name(self, instance):
if self.hide_input_bind:
self.hide_input.unbind_uid('text', self.hide_input_bind)
self.hide_input.text = instance.text
self.hide_input.focus = True
self.hide_input_bind = self.hide_input.fbind('text', self._update_text, instance)
def _update_text(self, button, instance, value):
button.text = value
class MyKivyApp(App):
def build(self):
return MyWidget()
def main():
Builder.load_string(kv_text)
app = MyKivyApp()
app.run()
if __name__ == '__main__':
main()
可在Android上运行的应用(Kivy Launcher):
App working on Android (Kivy Launcher):
这篇关于Python-Kivy-Builtdozer:让按钮调用android键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!