应用程序在Python2和Windows上的Windows上运行良好Python3,但收到错误:无法导入android,而Button无响应

时间:2017-10-10 11:06:16

标签: android python-2.7 python-3.x kivy buildozer

这是使用python 2.7kivy模块在​​numpy中编写的应用。我已按照此链接中的以下步骤安装了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似乎没有收到任何事件/响应。我可以对此有所反馈,谢谢。

当前已部分解决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();

1 个答案:

答案 0 :(得分:1)

  

无法导入&#34; android&#34;模块。

此消息与图像无关。如果您没有明确使用android模块,它将不会影响您的项目。要在日志中避免这一行,您只需要将android添加到buildozer.spec中的要求中。

  

读取文件时出错。\ 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)

<强> UPD:

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

看起来改变Window.size会以某种方式打破kivy触摸点检测。如果您想在Windows上更改窗口大小,use配置或换Window.size更改为platform check