【发布时间】:2020-08-18 00:53:44
【问题描述】:
我正在尝试开发一个使用谷歌翻译 API 的简单应用程序。该应用程序可以正确构建和安装,但是当我在手机上打开该应用程序时,它就崩溃了,没有任何类型的错误消息。
这是我的 main.py 文件:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from googletrans import Translator
Window.clearcolor = (0.2, 0.2, 0.2, 1)
Window.size = (450, 800)
tr = Translator()
# class in which we are defining action on click
class RootWidget(BoxLayout):
def translate(self, text, src, dest):
print(text, src, dest)
if text != "":
self.output_text.text = tr.translate(text, src=src, dest=dest).text
def speak(self):
print('speaking')
def read(self):
print('reading')
class TranslatorApp(App):
def build(self):
return RootWidget()
myApp = TranslatorApp()
myApp.run()
这是我的 translator.kv 文件:
<RootWidget>:
input_text:input_text
output_text:output_text
src_lang:src_lang
dest_lang:dest_lang
BoxLayout:
orientation: 'vertical'
spacing: 5
padding: 5
BoxLayout:
orientation: 'horizontal'
spacing: 5
size_hint: [1, 0.1]
Label:
text: 'Texto a traducir'
halign: 'left'
Label:
text: 'Texto traducido'
halign: 'left'
BoxLayout:
orientation: 'horizontal'
spacing: 5
size_hint: [1, 0.9]
TextInput:
id: input_text
color: [180, 180, 180]
size_hint: [0.5, 1]
TextInput:
id: output_text
text: "s"
color: [180, 180, 180]
size_hint: [0.5, 1]
BoxLayout:
orientation: 'horizontal'
spacing: 5
size_hint: [1, 0.2]
Button:
text: 'Hablar'
color: [0, 255, 255, .67]
background_normal: ''
background_color: [0, 0.5, 1, .85]
on_press: root.speak()
size_hint: [0.5, 1]
Button:
text:'Traducir'
color: [0, 255, 255, .67]
background_normal: ''
background_color: [1, 1, 1, 0.7]
on_press: root.translate(input_text.text, src_lang.text, dest_lang.text)
size_hint: [0.5, 1]
BoxLayout:
orientation: 'vertical'
spacing: 5
BoxLayout:
orientation: 'horizontal'
spacing: 5
Label:
text: 'Idioma de\norigen'
halign: 'left'
Label:
text: 'Idioma de\ndestino'
halign: 'left'
BoxLayout:
orientation: 'horizontal'
spacing: 5
TextInput:
id: src_lang
text: 'es'
TextInput:
id: dest_lang
text: 'en'
Button:
text: 'Leer traducción'
on_press: root.read()
我运行命令buildozer android debug 来构建应用程序。
我知道新的 p4a 工具链有 android_new,但是当我尝试时,我收到如下错误消息:
Unknown command/target android_new
我会添加 buildozer.spec 文件,但 stackoverflow 认为它是垃圾邮件
【问题讨论】:
-
也好久没
android_new了 -
您是否将 googletrans 库添加到您的规范文件中的要求中?
requirements = python3,kivy,googletrans。如果您将手机连接到电脑并启用 USB 调试,您可以在应用启动时看到日志。然后你可以使用命令buildozer android debug deploy logcat。编译完成后,您的应用会被推送到您的手机,您应该会在终端中看到日志信息。
标签: android python-3.x kivy buildozer p4a