Kivy应用在安卓手机上无法启动

0 投票
2 回答
2939 浏览
提问于 2025-04-18 16:22

我正在制作一个简单的应用程序,未来可以从网页上查看一些内容。目前在Windows上运行得很好(可以登录并显示新页面)。但是在我的手机上就不行了。它无法通过kivy启动器启动。我觉得它的大小应该没问题。我不知道问题出在哪里。我尝试了各种方法……

这是我的代码:

        #!/usr/bin/python
    # -*- coding: iso-8859-15 -*-
    import kivy
    kivy.require('1.8.0')
    import webbrowser
    import urllib.request
    import re

    from kivy.app import App
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.anchorlayout import AnchorLayout
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.properties import ObjectProperty, StringProperty 
    from kivy.network.urlrequest import UrlRequest
    from kivy.uix.popup import Popup

    from kivy.lang import Builder

    Builder.load_string("""
    <Phone>:
        sm: _screen_manager
        usernameField: username
        passwordField: password
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'top'
            ScreenManager:
                size_hint: 1, .9
                id: _screen_manager
                Screen:
                    name: 'Avakuva'             

                    Label: 
                        markup: True
                        text: '[size=50]Welcome to [color=8b00c3]Pelltech app![/color][/size]'
                    FloatLayout:
                        Button:
                            text: 'Info'
                            size_hint: .2, .1
                            pos_hint: {'x': .3, 'y': .1}
                            on_press: _screen_manager.current = 'Info'  
                        Button:
                            text: 'Login' 
                            size_hint: .2, .1
                            pos_hint: {'x': .5, 'y': .1} 
                            on_press: _screen_manager.current = 'Login'
                Screen:
                    name: 'Login'
                    GridLayout:
                        rows: 3
                        cols: 2
                        padding: 200
                        spacing: 10
                        Label:
                            text: 'User name:'
                        TextInput:
                            id: username
                        Label:
                            text: 'Password:'
                        TextInput:
                            id: password
                            password: True
                        Label:
                            text:''
                        Button:
                            text:"Log in"
                            on_press: root.goToBurners(username.text, password.text)

                Screen:
                    name: 'Info'
                    GridLayout:
                        rows:2
                        cols:1
                        padding: 200
                        spacing: 10
                        Label:
                            haling: 'justify'
                            text:'This app is for pelletburner owners. For more information contact:'
                        Button:
                            text:'Pelltech'
                            on_press: root.openPage()
                    FloatLayout:
                        Button:
                            text: 'Back'
                            size_hint: .2, .1
                            pos_hint: {'x': .4, 'y': .1}
                            on_press: _screen_manager.current = 'Avakuva'
                Screen:
                    name: 'Menu'
                    GridLayout:
                        rows:3
                        cols:1
                        padding:200
                        spacing: 10
                        Label:
                            markup: True
                            text: 'Menüü:'
                        Button:
                            text: 'Minu põletid'  
                            on_press: _screen_manager.current = 'põletid'
                        Button: 
                            text: 'Muu'

                Screen:
                    name: 'põletid'
                    GridLayout:
                        rows:4
                        cols:1
                        padding: 200
                        spacing: 10
                        Label:
                            markup: True
                            text: 'Burner 1'

                        Label:
                            markup: True
                            text: 'Burner 2'
                        Label:
                            markup: True
                            text: 'Burner 3'
                        Button:
                            text: 'Back'
                            size: 50, 50
                            on_press: _screen_manager.current = 'Menu' 
    """)

    class Phone(FloatLayout):
        def openPage(self):
            webbrowser.open('http://www.pelltech.eu')

        def goToBurners(self, username, password):
            #import pdb; pdb.set_trace()
            print("1")
            params = urllib.parse.urlencode({'username':username, 'password':password, 'nextPage':'/'})
            headers={'Content-type':'application/x-www-form-urlencoded','Accept':'text/plain'}
            print("2")
            req = UrlRequest('http://10.1.1.116/account/login?',req_headers=headers, req_body=params, method = "POST", on_redirect = self.gotResults, on_success=self.loginSuccess, on_failure=self.connectionFail)
            timeout = 10

        def newSuccess(self, req, result):
            print("new success")
            print(req.resp_status)
            print(result.decode())
            sm=ObjectProperty(ScreenManager())
            self.sm.current = 'Menu'

        def newFail(self, req, result):
            print("new fail")
            print(req.resp_status)
            print(result.decode())

        def newRedirect(self, req, result):
            print("new redirect")
            print(req.resp_status)
        print(result.decode())

    def gotResults(self, req, result):
        print(req.resp_status)
        print('on_redirect tõi siia') 
        #print(req.resp_headers)
        headerText = str(req.resp_headers)
        cookieStart = headerText.find('sessionid')
        #cookieEnd = headerText.find('expires')-2
        cookieEnd = headerText.find('=/')+2
        cookie = headerText[cookieStart:cookieEnd]
        print(cookie)
        headers={'Cookie': cookie}
        req = UrlRequest('http://10.1.1.116/',req_headers=headers, method = "POST", on_redirect = self.newRedirect, on_success=self.newSuccess, on_failure=self.newFail)

    def loginSuccess(self, req, result):
        print(req.resp_status)
        print("on_success t6i siia")
        print (result.decode())
        content = Button(text = 'Proovi uuesti!', size = (20,20))
        popup = Popup(title= 'Login ebaõnnestus!', content = content, size_hint = (None, None), size = (400,400), auto_dismiss = False)
        content.bind(on_press = popup.dismiss)
        popup.open()
        self.usernameField.text = ""
        self.passwordField.text = ""

    def connectionFail(self, req, result):
        print("on_failure t6i siia")
        print(req.resp_status)
        content = Button(text = 'Proovi uuesti!', size = (20,20))
        popup = Popup(title= 'Ühendus serveriga puudub!', content = content, size_hint = (None, None), size = (400,400), auto_dismiss = False)
        content.bind(on_press = popup.dismiss)
        popup.open()

class TestApp(App):
    def build(self):
        return Phone()

if __name__ == '__main__':
    TestApp().run()

2 个回答

0

我之前也遇到过同样的问题,我是通过在.spec文件中添加需求来解决的。

这些需求对我有效:

requirements = hostpython3==3.7.8,python3==3.7.8,kivy==1.11.1, certifi,chardet, lxml, docutils, future, idna, Kivy-Garden, Pygments, requests, six, soupsieve, urllib3, deep-translator, arabic-reshaper, python-bidi, openssl, pyopenssl, numpy, pytz, python-dateutil, pandas, setuptools, zope.interface, datetime

你需要把你的应用程序使用的所有模块和父模块都写在需求里。

要知道你的应用程序使用了哪些模块,有两种方法:

  1. 在当前应用程序文件夹中,打开powershell,运行命令 pip freeze。
  2. 按照下面的图片安装所有模块,这样可以帮助你安装应用程序所需的模块。

在这里输入图片描述

1

你应该按照 kivy文档中的调试说明去操作。如果这样做后问题还是不清楚,就把logcat的输出结果贴在这里。

撰写回答