kivy how to use online images in python - python

I'm trying to make a button which uses an image from URL.
icon= str('https://cdn2.iconfinder.com/data/icons/flat-ui-icons-24-px/24/eye-24-256.png')
self.add_widget(ImageButton(source=(icon), size=(100,100), size_hint=(0.1, 0.1), on_press=callback, pos_hint={"x":0.90, "top":1.0}))
class ImageButton(ButtonBehavior, Image):
pass
It shows as an white image for some reason. Could anyone help me please?

As eyllanesc comments, you must use AsyncImage subclass to load the image asynchronously from the server. Otherwise, the image will not be available when you instantiate the widget.
On the other hand, the code you show in your comment;
icon = AsyncImage(source='https://.../icon.png')
self.add_widget(ImageButton(source=(str(icon)))
is also incorrect, You are trying to pass to source (StringPropery) an instance of AsyncImage. The simple solution to that is to inherit from AsyncImage and not from Image:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import AsyncImage
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)
class ImageButton(ButtonBehavior, AsyncImage):
pass
class MainWindow(BoxLayout):
def __init__(self, **kwargs):
super(MainWindow, self).__init__(**kwargs)
icon = 'https://cdn2.iconfinder.com/data/icons/flat-ui-icons-24-px/24/eye-24-256.png'
self.add_widget(ImageButton(source=icon))
class Test(App):
def build(self):
return MainWindow()
if __name__ == "__main__":
Test().run()

Related

How to change screen by swiping in kivy python

I am trying to change to another screen by swiping the screen. I've tried carousel but it seems that it only works with images, so I've tried detecting a swipe motion and changing the screen after it has been detected.
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.carousel import Carousel
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
class HomeScreen(Screen):
def on_touch_move(self, touch):
if touch.x < touch.ox: # this line checks if a left swipe has been detected
MainApp().change_screen(screen_name="swipedhikr_screen") # calls the method in the main app that changes the screen
class ImageButton(ButtonBehavior, Image):
pass
class LabelButton(ButtonBehavior, Label):
pass
class SettingsScreen(Screen):
pass
class SwipeDhikrScreen(Screen):
pass
#def quit_verification():
# pop = Popup(title="verification", content=Label(text= "Are you sure?"))
GUI = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return GUI
def change_screen(self, screen_name):
# get the screen manager from the kv file
screen_manager = self.root.ids["screen_manager"]
screen_manager.transition.direction = "up"
screen_manager.current = screen_name
def quit_app(self):
MainApp().stop()
MainApp().run()
I got an attribute error: "None type object has no attribute 'ids'"
MainApp().change_screen(screen_name="swipedhikr_screen")
This line creates a new instance of MainApp, which doesn't have any widgets and therefore naturally fails when you try to access them.
Use the existing instance of MainApp, i.e. the one that you're actually running, via MainApp.get_running_app().
Also you are not correct that Carousel works only with images.

Kivy.uix.images is not working returns only blank screen

I'm runing "Kivy" on Windows 10. When I use "Images" in Kivy App... it returns Blank screen..
My code is Following(Code files and Images are in same folder)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.image import Image
class Game(Widget):
def __init__(self):
super(Game, self).__init__()
self.background = Image(source='backGround.jpg')
self.size = self.background.size
self.add_widget(self.background)
self.add_widget(Image(source='bird.jpg'))
class GameApp(App):
def build(self):
return Game()
if __name__ == "__main__":
GameApp().run()
Please Someone point out Mistake... I'll be greatful... Regards

Accessing "height" property of an instance of a widget

I need to access the height of a widget after it is created. However, whenever I tried to access my_widget.height, it returns the default height of 100 rather than the actual height.
Retrieving self.height inside the on_touch_down definition of the my_widget class works though.
My problem is really best described in the following code.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Layout(BoxLayout):
# the following yields the correct height of Layout,
# which is the same as the height of the Window
def on_touch_down(self, touch):
print("inside Layout via on_touch_down:", self.height)
class MyApp(App):
def build(self):
l = Layout()
# However, accessing the height here yields the
# the default height 100
print("inside MyApp:", l.height)
return l
if __name__ == "__main__":
MyApp().run()
I spent hours reading different parts of the API doc and the guides on the Kivy website, but couldn't quite find what I missed.
In the build() method the application has not yet been built so the widget has its default value, so you must obtain that information a moment later, for this there are the following options:
Use App.on_start():
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Layout(BoxLayout):
pass
class MyApp(App):
def build(self):
l = Layout()
return l
def on_start(self):
print(self.root.height)
if __name__ == "__main__":
MyApp().run()
Use Clock.schedule_once():
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
class Layout(BoxLayout):
pass
class MyApp(App):
def build(self):
l = Layout()
Clock.schedule_once(self.after_build)
return l
def after_build(self, dt):
print(self.root.height)
if __name__ == "__main__":
MyApp().run()

FactoryException when trying to create a "IconButton" in Kivy

I've tried to create an Icon that can be clicked, which means an Image with a ButtonBehavior.I followed the documentation (http://kivy.org/docs/api-kivy.uix.behaviors.html), and I've got a FactoryException with the following code:
# coding: utf-8
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.image import Image
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
kv_string = """
BoxLayout:
IconButton
"""
class IconButton(ButtonBehavior, Image):
def on_press(self):
print("on_press")
class DashboardApp(App):
pass
Builder.load_string(kv_string)
if __name__ == "__main__":
DashboardApp().run()
When I change the parent class of IconButton from (ButtonBehavior, Image) to (ButtonBehavior, Widget), the problem disappears.
You want kivy.uix.image, not kivy.core.image.

What is a root widget and how to implement it in Kivy Python ?

Here is my code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.image import Image as CoreImage
class WeaselApp(App):
def __init__(self, image):
self.image = image
def coreimage(self, load_image):
self.load_image = load_image
load_image = CoreImage("psychTREE.jpg")
#return load_image
def built(self):
return coreimage(self, load_image)
if __name__== "__main__":
WeaselApp(App).run()
When I run it, it says " WeaselApp has no attribute 'root' ". Why is this the case? Any hints or suggestions are greatly appreciated.
You don't return any widget from your build method, for two reasons:
As Joran Beasley said, you need a build method, not built.
CoreImage is not a widget that can be displayed on screen, it's a low level tool to load image data. You should use a kivy.uix.image.Image widget.
Edit: Following the comments, replace your code with this:
from kivy.app import App
from kivy.uix.image import Image
class YourApp(App):
def build(self):
return Image(source='psychTREE.jpg')
YourApp().run() # edited this in after

Categories

Resources