App works well on Windows in Python2 & Python3, bu

2019-06-06 14:16发布

This is an app written in python 2.7 using kivy and numpy modules. I have installed buildozer by following steps in this link : https://github.com/kivy/buildozer , i did not do sudo pip install https://github.com/kivy/buildozer/archive/master.zip. After deploying the app on android buildozer android deploy run logcat i saw some errors on the log :

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.

Problem : The Image widget does not show, and the touch for the button does not seem to receive any event/response. May I have feedbacks on this, thanks.

Current partially solved : The Image widget now shows, I change the source address from .\\logo_example1.png to logo_example1.png.

Existing problem : the Button widget is still unresponsive to touch on android, but works fine on Windows.

Code to test Start and Back buttons :

This is the 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()

This is the 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();

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-06 15:05

Failed to import "android" module.

This msg has nothing to do with image. It's won't affect your project if you don't use android module explicitly. All you need to do to avoid this line in logs is to add android to requirements inside buildozer.spec.

Error reading file .\logo_example1.png

This error indicates that image can't be found. I don't know what's problem with .\\logo_example1.png, but it's better just to use absolute path's and forget about problems.

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)

Upd:

Problem with pressing is related to this line - Window.size = (600, 700). Remove it and everything will work.

Looks like changing Window.size somehow breaks kivy touch point detection. If you want to change window size on Windows, use Config or wrap Window.size changing with platform check.

查看更多
登录 后发表回答