我正在制作一个简单的应用程序(将来)从网页上看到一些东西。目前它在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()