在Kivy Launcher中查看Android上的打印语句输出
我写了一个程序,它在运行时会在标准输出上打印一些指令。在Windows上运行这个应用时,我可以看到这些输出,但在我的三星S3安卓设备上运行同样的应用时,我却看不到任何打印的内容。
有时候我们可以在设备上看到一个.kivy的文件夹,它和程序在同一个目录下,但那些日志文件里也只包含了与kivy相关的日志,完全没有打印语句的输出。
有没有人能给点建议,告诉我该怎么用这个呢……
3 个回答
没错,logcat 这个工具是用来处理 Java 的日志的,它不会捕捉到 Python 的 print 语句。要想方便地使用 .kivy/logs 这个文件,你需要在手机上获得 root 权限。如果你的手机比较新,而且不想因为获取 root 权限而影响保修,那你可以使用 Python 的 warnings 模块,这样就能让 logcat 访问到 Python 的输出:
import warnings
warnings.warn('print a custom message')
或者你也可以使用像 pyjnius 这样的 Java/Python 包来创建一个 Java Logger:
from jnius import autoclass
Logger = autoclass('java.util.logging.Logger')
mylogger = Logger.getLogger('MyLogger')
mylogger.info('print a message')
Kivy启动器会忽略print()
这个命令。所以,建议你用logging.info()
来代替。
在main.py文件中:
import logging
....
logging.info('any strings you want to output')
日志文件会保存在.../kivy/your_app/.kivy/logs/
这个路径下。
使用adb的logcat功能可以获取你应用的输出,或者你也可以使用一些在线的应用来帮助显示你的日志,并搜索'Python'。
以上步骤的详细说明:
在你的设备上启用开发者选项(可以在网上查一下怎么做)。然后开启USB调试。
图片来源于http://androidfannetwork.com/
接着,用USB线把你的设备连接到电脑,然后在控制台输入adb devices
。这时应该能看到你的设备(可能会弹出一个提示,询问你是否允许连接到电脑)。
还有一个更简单的方法是使用小部件上的视觉指示,而不是在控制台上打印信息。你可以为你的应用创建一个函数bubprint
。
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.factory import Factory
from kivy.lang import Builder
Builder.load_string('''
<InfoBubble@Bubble>
# declare our message StringProperty
message: 'empty message'
# let the bubble be of 200 device pixels
# and expand as necessary on the height
# depending on the message + 20 dp of padding.
size_hint: None, None
show_arrow: False
pos_hint: {'top': 1, 'right': 1}
size: dp(200), lbl.texture_size[1] + dp(20)
Label:
id: lbl
text: root.message
# constraint the text to be displayed within
# the bubble width and have it be unrestricted
# on the height.
text_size: root.width - dp(20), None
''')
def bubbprint(self, message):
message = repr(message)
if not self.info_bubble:
self.info_bubble = Factory.InfoBubble()
self.info_bubble.message = message
# Check if bubble is not already on screen
if not self.info_bubble.parent:
Window.add_widget(self.info_bubble)
# Remove bubble after 2 secs
Clock.schedule_once(lambda dt:
Window.remove_widget(self.info_bubble), 2)