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)
Related
I am using Kivy Carousel to build an App.
However I would like to maintain Manual control of the carousel and disable the swipe action (I will manually call carousel.load_next)
I have looked through the Documentation but cannot see any way to disable the swipe action.
If anyone could help me I would appreciate it.
Many thanks,
Seotha.
You can disable the user swipe by controling the scroll_timeout. If you simply set it to 0 the user will be unable to trigger the scroll event.
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', scroll_timeout=0)
for i in range(10):
src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i
image = AsyncImage(source=src, allow_stretch=True)
carousel.add_widget(image)
return carousel
CarouselApp().run()
Thanks Authur, I will mark as Answer.
I also discovered I can subclass Carousel and override on_touch_move with nothing.
class MyCarousel(Carousel): def on_touch_move(self,touch): pass
The following might be helpful:
use
scroll_distance: '<x>dp'
I have an image located relative to the screen size with RelativeLayout. How can I get the global (/window) position of that image, so that I can detect if touch coordinates overlap the image?
this is my current implimentation:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.relativelayout import RelativeLayout
class mainApp(App):
def build(slef):
appRoot = FloatLayout()
relPos = RelativeLayout()
button = Image()
relPos.pos_hint = {"right":1,"top":1.5}
relPos.add_widget(button)
#attempting to print the coordinates of the Image in the window
print(relPos.to_parent(*button.pos))
return relPos
if __name__ == '__main__':
mainApp().run()
As you can see, I'm attempting to use to_parent to get the position relative to the parent. However, I don't think it works pos_hint.
I've also tried using AnchorLayout, and have had similar issues
Note: while I know I can use the window_size, allong with a bit of math, I would like a more robust solution that can handle more complex situations
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()
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.