App works well on Windows in Python2 & Python3, but receiving Error : failed to import android, and Button unresponsive

App works well on Windows in Python2 & Python3, but receiving Error : failed to import android, and Button unresponsive

这是一个使用 kivynumpy 模块用 python 2.7 编写的应用程序。我按照 link 中的步骤安装了 buildozerhttps://github.com/kivy/buildozer,我没有这样做 sudo pip install https://github.com/kivy/buildozer/archive/master.zip。在 android buildozer android deploy run logcat 上部署应用程序后,我在日志中看到了一些错误:

10-10 17:44:49.497 19176 19207 I python  : [ERROR  ] [Image       ] Error reading file .\logo_example1.png
10-10 17:44:49.498 19176 19207 I python  : [WARNING] [Base        ] Unknown <android> provider
10-10 17:44:49.498 19176 19207 I python  : [INFO   ] [Base        ] Start application main loop
10-10 17:44:49.503 19176 19207 I python  : [INFO   ] [GL          ] NPOT texture support is available
10-10 17:44:49.504 19176 19207 I python  : 0
10-10 17:44:49.504 19176 19207 I python  : coloring
10-10 17:44:49.505 19176 19207 I python  : [ERROR  ] [Base        ] Failed to import "android" module. Could not remove android presplash.

问题:Image 小部件不显示,按钮的 touch 似乎没有收到任何 event/response。我可以对此有反馈吗,谢谢。

当前部分解决: Image 小部件现在显示,我将源地址从 .\logo_example1.png 更改为 logo_example1.png

现有问题: Button 小部件仍然没有响应触摸 android,但在 Windows 上工作正常。

测试开始和返回按钮的代码:

这是 main.kv :

#: import Main main    
<CtmButton@Button>:
    font_size:30
    size:100, 50
<BackButton@Button>:
    font_size:30
    size:100, 50
    text: "Back"
    pos: Main.Wsize[0]+100, Main.Wsize[1]+100
<mainWidget>:
    id: main_widget
<homeWidget>:
    id: home_widget
    padding : 200
    Image:
        id:logo
        source: 'logo_example1.png'
        center: 300, 450
    CtmButton:
        id: start_button
        text: "Start"
        center:300, 200
        on_release: home_widget.startbutton_function()
    ## I also have tried replacing this with on_touch_down
    ## to see whether it will solve the problem
<puzzleWidget>:
    id: puzzle_widget
    BackButton:
        id: back_button_1
        center: 100, 37.5
        on_release: puzzle_widget.backbutton_function()

这是main.py:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.core.image import ImageData
from kivy.clock import Clock
from kivy.graphics import Rectangle
import time
import math
import itertools
import numpy 
Window.clearcolor = (0.2, 0.25, 0.2, 0.5); col_dx = 0.005; y = list(Window.clearcolor); xstart = 0;
Window.size = (600, 700); Wsize = Window.size;
class mainWidget(Widget):
    def __init__(self):
        Widget.__init__(self);
        home = homeWidget(); self.add_widget(home);
class homeWidget(Widget):
    def __init__(self):
        Widget.__init__(self);
        self.bg_animate();
    def printingsomething(self, dt):
        print(dt);
    def bg_animate(self):
        global bg_anim
        self.bg_anim = Clock.schedule_interval(self.printingsomething,1/40);
        self.bg_anim();
    def startbutton_function(self):
        print('start pressed');
        self.bg_anim.cancel();
        mainw.clear_widgets();
        mainw.add_widget(puzzleWidget());
class puzzleWidget(Widget):
    def __init__(self):
        Widget.__init__(self); 
        self.Bar1 = Image(size = [600, 75], color = list(Window.clearcolor)); self.Bar1.color[-1] = 0.8; self.Bar1.pos = [0, 700 - self.Bar1.size[1]];
        self.Bar2 = Image(size = [600, 75], color = list(Window.clearcolor)); self.Bar2.color[-1] = 0.8; self.Bar2.pos = [0, 0];
        #### ADD WIDGET(S):
        self.add_widget(self.Bar1); self.add_widget(self.Bar2); 
        self.remove_widget(self.ids.back_button_1); self.add_widget(self.ids.back_button_1);
    def backbutton_function(self):
        print('back pressed');
        self.clear_widgets();
        mainw.clear_widgets();
        mainw.add_widget(homeWidget());
################## main window for the app #########################
class mainApp(App):    
    def build(self):
        global mainw
        mainw = mainWidget();
        return mainw
####################################################################
mainApp = mainApp()
mainApp.run();

Failed to import "android" module.

此消息与图片无关。如果您不显式使用 android 模块,它不会影响您的项目。要避免在日志中出现这一行,您需要做的就是将 android 添加到 buildozer.spec.

内的要求中

Error reading file .\logo_example1.png

此错误表示找不到图像。我不知道 .\logo_example1.png 有什么问题,但最好只使用绝对路径并忘记问题。

import os

root_dir = os.path.dirname(os.path.abspath(__file__))
img_rel = 'logo_example1.png'
img_abs = os.path.join(root_dir, img_rel)

print(img_abs)

更新:

按下问题与此行有关 - Window.size = (600, 700)。删除它,一切都会正常。

看起来改变 Window.size 以某种方式破坏了 kivy 触摸点检测。如果您想在 Windows、use Config or wrap Window.size changing with platform check.

上更改 window 大小