Please forgive me I am in my infancy learning Python and started using Kivy not too long ago.
I am simply trying to add photos from my "carousel/" directory in my project to be each photo that comes up in the carousel app one by one when it loads.
The code runs fine, and I am even able to load Async photos with the link to the photo, but for whatever reason when I try to load photos from my "carousel" folder they don't show up.
I know that these photos return otherwise because I used the "Image" call and it worked, I also looked all over and there were other solutions but I could not make the connection between their solution and what it is I need.
Here is the code and a picture below, like I said the code builds and runs fine but the pictures won't show up. Thank you in advance!
[from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from kivy.core.image import Image
from kivy.factory import Factory
class CarouselApp(App):
def build(self):
carousel = Carousel(direction='right')
for i in range(0,2):
src = "carousel/%s.jpg" % str(i)
image = Factory.AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
CarouselApp().run()][1]
https://i.stack.imgur.com/igyeq.png
You have to store all your pictures in a folder called carousel, and you can remove the Factory.
Example
main.py
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
class CarouselApp(App):
def build(self):
carousel = Carousel(direction='right')
for i in range(0, 6):
src = "carousel/%s.png" % str(i)
image = AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
CarouselApp().run()
Output
You are trying to load 'carousel/0.jpg' and 'carousel/1.jpg' instead of 'carousel_images/00.jpg' and 'carousel_images/01.jpg', Try this:
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from kivy.core.image import Image
from kivy.factory import Factory
class CarouselApp(App):
def build(self):
carousel = Carousel(direction='right')
for i in range(0,2):
src = "carousel_images/0{}.jpg".format(str(i))
image = Factory.AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
CarouselApp().run()
Related
This is a kivy python script with carousel I found in the web which I am trying to replicate.
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.factory import Factory
from kivy.uix.image import Image
class Example1(App):
def build(self):
carousel = Carousel(direction='right',loop='true')
for i in range(1,5):
src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i
#load images asynchronously
image = Factory.AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
print(i)
return carousel
if __name__ == '__main__':
Example1().run()
This downloads all the images at once which works well for smaller sized and lesser number of images. When I tried it in other larger number of images with consideraly larger size. It took a long time to even load those images to kivy app.
Is there a way that we can load the images one by one? Say when we run the kivy app instead of downloading all images together; the first slide of carousel should only download the first image and when we swipe left or right the corresponding slides image should download.
you can create a custom generator function to load the previous or the next slide when called which then downloads or loads the respective image. this a script in my app which automatically changes the screen after every 3 seconds that but i created the carousel in my kv file manually instead of calling them from a remote source.
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
import threading
from kivy.clock import Clock
class StartScreen(Screen):
ml = ObjectProperty(None)
scroller = ObjectProperty(None)
button = ObjectProperty(None)
carousel = ObjectProperty(None)
def caller(self):
threading.Thread(target = self.call).start()
def call(self):
Clock.schedule_interval(self.changer, 3)
def changer(self,*args):
self.ids.carousel.load_next()
I created a simple text-to-speech app with Kivy, using the FloatLayout option but am having trouble changing the color of the GUI without actually creating a .kv file (which I do not wish to do). The code of my app is here:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
import requests
from threading import Thread
import os
class ButtonApp(App):
def talk(self):
self.info.text = self.text.text
command = "say %s" % (self.text.text)
os.system(command)
def say(self,instance):
t = Thread(target=self.talk)
t.start()
def build(self):
self.b = FloatLayout()
self.info = Label(text="Hello!", pos=(20,400) ,size_hint=(1,0.5), font_size="40sp")
self.text = TextInput(text='Hello!', pos=(20,200), size_hint=(1,0.5))
self.submit = Button(on_press=self.say,text='Submit',pos=(20,100), size_hint=(1,0.5))
self.b.add_widget(self.info)
self.b.add_widget(self.text)
self.b.add_widget(self.submit)
self.b.bind()
return self.b
if __name__ == "__main__":
ButtonApp().run()
Like I mentioned beforehand, all the suggestions I found doing prior research involved either Canvas (which I am not using), or creating a .kv file. Is there a pure python-kivy method of changing the color of a GUI?
You can do anything in pure python, though the reason you see so many kv examples is because it's easier and more concise due to being a more domain specific language, so I don't recommend avoiding it.
What kind of change do you actually want to make? For instance, you can change the background image of the Button with the background_normal or background_down properties (which take a filepath to an image), or tint its colour by setting its background_color to e.g. (1, 0, 0, 1) for red.
I'm having trouble loading my image into the GUI for Python using Kivy.All it shows is a white screen. Any help would be appreciated.
from kivy.app import App
from kivy.uix.image import Image
class TheApp(App):
def build(self):
image = Image(source= "psychTREE.jpg")
return image
if __name__ == "__main__":
TheApp().run()
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
I made a little Kivy application as an interface. The carousel seems to be working, but I would like to make the carousel start by itself (that means without using a mouse). In fact, the app will be deployed on a little robot that does not have any mouse or keybord, thus that is why I need the carousel passing the images by itself.
As you can see in my code, the carousel does work, but I just can't figure out how to make it start without clicking on it with a mouse.
class Logo(App):
def build(self):
carousel = Carousel(direction='right')
for i in range(2):
src = "image.png"
image = Factory.AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
if __name__ == '__main__':
Logo().run()
Any ideas ? Thanks.
You want an event to trigger the movement, i think the easiest way would be to use a Clock.
from kivy.clock import Clock
then in your build, you could schedule incrementing the position of the carousel at some interval, carousel has a convenient function for that load_next, but if you just do that, it'll get stuck on the last frame pretty fast, so you'll probably want to pass loop=True to the Carousel instantiation.
from kivy.app import App
from kivy.factory import Factory
from kivy.uix.carousel import Carousel
from kivy.clock import Clock
class Logo(App):
def build(self):
carousel = Carousel(direction='right', loop=True)
for i in range(2):
image = Factory.Label(text=str(i))
carousel.add_widget(image)
Clock.schedule_interval(carousel.load_next, 1)
return carousel
if __name__ == '__main__':
Logo().run()
(i changed the images by labels to see the difference more easily)