Kivy - Urlrequest works on my computer but not on my phone - python

I have made a simple app that uses urlrequest and the Wikimedia api to get some information from Wikipedia. While this perfectly works on my computer (it shows the information), it does not on my phone (it shows nothing).
The main script:
from kivy.network.urlrequest import UrlRequest
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.properties import StringProperty
class WikiApp(App):
summary = StringProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.summary = ""
def results(self, req, result):
self.summary = str(result["query"]['searchinfo']['totalhits'])
def build(self):
return MainScreen()
def generate(self):
req = UrlRequest('https://en.wikipedia.org//w/api.php?action=query&format=json&list=search&srsearch=abc', self.results)
class MainScreen(BoxLayout):
"""Class for the main screen widget"""
pass
and the kv file:
<MainScreen>:
orientation: 'vertical'
BoxLayout:
Label:
id: SummaryLabel
text: app.summary
size_hint_y: None
text_size: self.width, None
height: self.texture_size[1]
BoxLayout:
Button:
id: generate
text: "Generate"
on_release: app.generate()
Why is it not working on my phone ? Is there something I need to specify in the spec file ?
Thank you for your help !

Try import certifi then add another parameter to your UrlRequest function -- ca_file=certifi.where()
For Example:
req = UrlRequest('https://en.wikipedia.org//w/api.php?action=query&format=json&list=search&srsearch=abc', self.results, ca_file=certifi.where())

You should put INTERNET permission in the buildozer.spec file:
android.permissions = INTERNET
And after launch of the app, manually give the permission for internet going to the app info. If you want the app to prompt for the permission then you need to implement request_permissions, check_permission methods in your code.

Related

Can someone provide a working code of loading data of text file to Label of kivy

I am able to get the data in main app class and run it to get as Label but when I want to do the same by getting data in main app class where as defining Label in another class, it's giving name error.
import kivy
from kivy.app import App # import Kivy App module to create
from kivy.uix.label import Label # import Label Module
kivy.require('1.11.1')
class MyKivyApp(App):
def build(self):
f=open('tesit.txt','r')
t=f.read()
return Label(text =t)
rt=MyKivyApp()
rt.run()
Your lines:
rt=MyKivyApp()
rt.run()
are inside the MyKivyApp. Just unindent those two lines to get them outside of the App class.
This's how we can read a text file and display it as label in another screen in kivy.
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivy.properties import StringProperty
class Screen1(Screen):
f=open('test.txt','r')
t=f.readlines()
b=t[0]
f.close()
class Screen2(Screen):
username = StringProperty("")
root = Builder.load_string('''
<Screen1>:
name:'xy'
id:loginscreen
BoxLayout:
orientation: "vertical"
Button:
text:'Execution button'
on_release: root.manager.current = "screen2"
<Screen2>:
name: "screen2"
FloatLayout:
Label:
text: root.username
Button:
text:'Back to menu'
size_hint:.3,.3
on_release: root.manager.current = "xy"
ScreenManager:
Screen1:
id:loginscreen
Screen2:
username: loginscreen.b
''')
class MyApp(App):
def build(self):
return root
r=MyApp()
r.run()

How do I make a Kivy app update values from other files?

I'm trying to make a Kivy interface that updates constantly when values imported from other files are constantly changing.
Questions:
How do I make it so, when I change the value of the "fk" variable in
test.py, that the Kivy app updates, without me having to restart the
program?
What is the use of "AnchorLayout" in class Interface(AnchorLayout): ?
I've tried to Google this, and still not 100% sure. Why put self in def build():?
What is the use of self.update in the code?
Questions 2 and 3 are unrelated to my main questions, but if anyone can help, that would be great.
What I have tried and failed:
main.py:
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang import Builder
import test123
Builder.load_file('main.kv')
Builder.load_file('robotinformation.kv')
class Interface(AnchorLayout):
val = NumericProperty(0)
class InterfaceApp(App):
def build(self):
Clock.schedule_interval(self.update, 1)
return Interface()
def update():
val = test123.numval()
if __name__ == '__main__':
InterfaceApp().run()
main.kv:
<Interface>:
BoxLayout:
orientation: 'vertical'
StackLayout:
orientation: 'tb-rl'
RobotInformation:
id: _robot_information
...
robotinformation.kv:
<RobotInformation#BoxLayout>:
BoxLayout:
orientation:'vertical'
Label:
text: app.val
...
test123.py:
def numval():
fk = 101
return fk

Python 3.7, Change button's colour when it's down

How do I change the button's color when it's down? As in, when you press the button, replace the blue-ish colour into another one?
I've tried fiddling around with background_color_down, background_color_normal, I tried using canvas and the sort but nothing seems to have the effect I intend it to
Or you could try giving the path to a png file with your design of the pressed button to the background_down attribute. Here my file is called "pressed.png" and is located within the same folder as the python programm. this is a link to something i made within 30 seconds in inkscape.
#!/usr/bin/python3.5
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty, ListProperty
from kivy.atlas import Atlas
kv= Builder.load_string('''
<MainScreen>
my_but:my_but
orientation: "vertical"
size_hint:1,1
# size_hint: 0.3,0.3
# pos_hint:{"center_x": 1, "center_y":1}
Label:
size_hint: 0.5,0.4
text: "Look at my Button go:"
Button:
id:my_but
size_hint: 0.5,0.4
text: "klick me"
# background_normal: ""
background_down: "pressed.png"
''')
class MainScreen(BoxLayout):
my_but = ObjectProperty(None)
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
class myApp(App):
def build(self):
return MainScreen()
if __name__ == "__main__":
myApp().run()

how do you change screens from python file in KIVY

All the tutorials in the internet show how to use the ScreenManager and change different screens from the .kv files.
I am still learning and I can not manage to control it from the .py file.
I want my app first to perform some validation before starting. There fore i want Kivy app to start with a simple "Please wait" screen, and after that to go to the Main screen.
Please help!
Here is a short example of how one can change a screen from the python code
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_string("""
<Screen1>:
name: 'screen1'
Button:
on_press: root.on_click()
<Screen2>:
name: 'screen2'
Label:
text: "Cool!"
<MyScreenManager>:
Screen1:
Screen2:
""")
class Screen1(Screen):
def on_click(self):
sm.current = 'screen2'
class Screen2(Screen):
pass
class MyScreenManager(ScreenManager):
pass
class SimpleApp(App):
def build(self):
global sm
sm = MyScreenManager()
return sm
SimpleApp().run()
I have used the button on_press event to call the python code but you can run your validations in another thread and when your are done you can call back to the kivy code using the Clock api

How to get console output printed using kivy

I have been trying to learn how to use Kivy python and I would like to
Know how to interact with the Os console/terminal to run commands and
receive results. The tutorials I have seen so far only show how to create
widgets,buttons,etc
For example how do You get the result from running the command "uname"
displayed in kivy. With such code below. using "on press". How can I let it interact with the OS run a command and display it back in the kivy app. Is there any tutorial on creating desktop apps/utilities
from kivy.app import App
from kivy.uix.button import Button
class tutap(App):
def build(self):
return Button(text="Press here")
tutap().run()
Update:
Here is example of what Im trying to achieve.This uses the easygui module:
import subprocess
from easygui import *
msg= "what you want"
out = subprocess.check_output("uname -a",shell=True)
title = "My choice"
choices=["kernel version","nothing"]
choice=boolbox(msg,title,choices)
if choice==1:
msgbox(out)
elif choice==0:
msgbox("The End")
The simplest way I can think to do this is just make a function that writes to a file at the top of your file like this
def printlog(message):
with open('./log.txt','a') as f: f.write(message+"\n")
then in your program when ever you want the print out put simply put printlog("whatever you wanted printed!")
The file will be saved in the same folder as your program.
More of a you can theoretically open it while the program is running, but this is more useful as a postmourtum.
I don't really see the point of doing something like that but if you want you can call App.run() method in separate thread so it won't block command line.
An example using cmd module:
import logging
logging.getLogger("kivy").disabled = True
from kivy.app import App
from kivy.uix.listview import ListView
from cmd import Cmd
from threading import Thread
class MyApp(App):
def build(self):
self.lv = ListView()
return self.lv
def update(self, line):
self.lv.adapter.data.append(line)
return "list updated"
class MyCmd(Cmd, object):
def __init__(self, app, *args):
super(HelloWorld, self).__init__(*args)
self.app = app
def do_EOF(self, line):
self.app.stop()
return True
def default(self, line):
ret = self.app.update(line)
print(ret)
if __name__ == '__main__':
app = MyApp()
Thread(target=app.run).start()
MyCmd(app).cmdloop()
Here I how I went about getting console command output.
The python code first:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
import subprocess
class shellcommand(BoxLayout):
first=ObjectProperty()
second=ObjectProperty()
third=ObjectProperty()
def uname(self):
v=subprocess.check_output("uname -a",shell=True)
result=Popup(title="RESULT",content=Label(text="kernel is\n" + v))
result.open()
def date(self):
d=subprocess.check_output("date",shell=True)
res=Popup(title="DATE",content=Label(text="the date today is\n" + d))
res.open()
def last(self):
last=subprocess.check_output("w",shell=True)
ls=Popup(title="LOGIN",content=Label(text="logged in \n" + last))
ls.open()
class shellApp(App):
def build(self):
return shellcommand()
shellApp().run()
And then the kivy file named shellapp.kv
<shellcommand>:
orientation: "vertical"
first:one
second:two
third:three
canvas:
Rectangle:
source: "snaps.png" #location of any picture
pos: self.pos
size: self.size
BoxLayout:
orientation: "horizontal"
Button:
id:one
text: "UNAME"
background_color: 0,0,0,1
font_size:32
size_hint:1,None
on_press: root.uname()
Button:
id:two
text: "DATE"
background_color: 1,1.5,0,1
font_size:32
size_hint:1,None
on_press: root.date()
Button:
id: three
text: "LOGGED IN"
background_color: 1,0,0,1
font_size:32
size_hint: 1,None
on_press: root.last()
If there is a way to improve this code please let Me know how to.Thanks

Categories

Resources