If you are going to display an image from a URL I recommend using the AsyncImage widget. You can pass the url as the source.
Rather that requests library, use kivy url request module. https://kivy.org/doc/master/api-kivy.network.urlrequest.html?highlight=url#module-kivy.network.urlrequest
The requests library is blocking, the kivy URLrequest calls are non-blocking and integrated into kivy event loop.
Here are two examples, one that uses the URLRequest module, the other uses the AsyncImage Widget.
# Example using URLRequests
from kivy.app import App
from kivy.lang import Builder
from kivy.event import EventDispatcher
from kivy.network.urlrequest import UrlRequest
from kivy.properties import ListProperty
from kivy.clock import Clock
kv = """
BoxLayout:
orientation: 'vertical'
Label:
id: label
text_size: self.size
valign: 'center'
halign: 'center'
padding: 10,10
font_size: 40
Button:
size_hint_y: None
height: 48
text: 'Get Cat Facts'
on_release: app.aw.check_net()
"""
class AccessWeb(EventDispatcher):
facts = ListProperty()
def check_net(self):
UrlRequest('https://cat-fact.herokuapp.com/facts', on_success=self.net_success, on_failure=self.net_fail)
def net_success(self,req, r):
print(f'Success: {req.is_finished}')
self.facts.clear()
for fact in r:
self.facts.append(fact['text'])
print(self.facts)
app = App.get_running_app()
app.root.ids.label.text = self.facts[0]
def net_fail(self, req, r):
print("Fail:", r)
class TestAccessWebApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cnt = 0
self.aw = None
def build(self):
self.aw = AccessWeb()
return Builder.load_string(kv)
def on_start(self):
Clock.schedule_interval(self.display_cat_fact, 5)
def display_cat_fact(self, dt):
if self.aw.facts:
self.root.ids.label.text = self.aw.facts[self.cnt]
self.cnt = (self.cnt+ 1) % len(self.aw.facts)
TestAccessWebApp().run()
# Example using AsyncImage
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import StringProperty
kv = """
<ReadWriteScreen>:
BoxLayout:
orientation: 'vertical'
Label:
text: root.movie_title
size_hint_y: None
height: 48
font_size: 24
AsyncImage:
source: root.source
Button:
text: 'Go Back'
size_hint_y: None
height: 48
on_release: root.manager.current = 'title'
<TitleScreen@Screen>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Home Screen'
Button:
text: 'Go to Movie'
size_hint_y: None
height: 48
on_release: root.manager.current = 'movie'
ScreenManager:
TitleScreen:
name: 'title'
ReadWriteScreen:
name: 'movie'
"""
class ReadWriteScreen(Screen):
movie_title = StringProperty()
source = StringProperty()
def on_pre_enter(self, *args):
self.movie_title = 'Interstellar'
self.source = 'https://m.media-amazon.com/images/M/MV5BZjdkOTU3MDktN2IxOS00OGEyLWFmMjktY2FiMmZkNWIyODZiXkEyXkFqcGdeQXVyMTMxODk2OTU@.jpg'
class MovieApp(App):
def build(self):
return Builder.load_string(kv)
MovieApp().run()
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/932ff16b-d722-4326-823b-5588443f884an%40googlegroups.com.