Get global position of widget in Kivy? - python

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

Related

How to load image when the corresponding carousel slide is active?

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()

Export_to_png only saves background

I recently learnt about the Python framework kivy and started to follow the simple paint app tutorial. Now I want to save what was drawn as a .png file but instead of the colored dots it only exports the black background.
This is my code example:
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
class MyPaintWidget(Widget):
def on_touch_down(self,touch):
color=(0,0,1)
with self.canvas:
Color(*color)
d=30.
Ellipse(pos=(touch.x-d/2., touch.y-d/2.), size=(d,d))
class MyPaintApp(App):
def build(self):
parent=Widget()
self.painter=MyPaintWidget()
ssbtn=Button(text='Save')
ssbtn.bind(on_release=self.save_screenshot)
parent.add_widget(self.painter)
parent.add_widget(ssbtn)
return parent
def save_screenshot(self,obj):
self.painter.export_to_png("screenshot.png")
if __name__ == '__main__':
MyPaintApp().run()
What am I doing wrong? Thanks for your help.
You are missing one thing - Widget won't set its size and position automatically to whole Window size, but only to default ones i.e. pos = [0, 0] size = [100, 100] because Window != Layout and even adding it to something that doesn't inherit from a Layout won't make it better (Widget != Layout) which you can easily see if you look a that saved png (is too small).
self.painter=MyPaintWidget(size=[800,600])
will make the job for you if you plan to stay only on pc and only with the default Window size. If not, then use:
from kivy.core.window import Window
self.painter=MyPaintWidget(size=Window.size)
which seems to be more practical, yet you're still not there. An optimal solution would be using a Layout which does these things for you such as BoxLayout, GridLayout or some others.
And a perfect solution for you would be to use a StencilViewin a Layout, which when is placed won't take a screenshot of the whole canvas, but only the part you'd think it'll take i.e. the part sized as the Widget itself placed on its position.
Try drawing outside of the red box. Inspector will provide you with showing widgets in a color and other functions, so definitely try it as it was mentioned in the comments.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.stencilview import StencilView
from kivy.graphics import Color, Ellipse, Line, Rectangle
class MyPaintWidget(StencilView):
def on_touch_down(self,touch):
color=(0,0,1)
with self.canvas:
Color(*color)
d=30.
Ellipse(pos=(touch.x-d/2., touch.y-d/2.), size=(d,d))
class MyPaintApp(App):
def build(self):
parent=Widget()
self.painter=MyPaintWidget(size=[i/2.0 for i in Window.size])
with self.painter.canvas:
Color(1, 0, 0, 0.3)
Rectangle(pos=self.painter.pos, size=self.painter.size)
ssbtn=Button(text='Save')
ssbtn.bind(on_release=self.save_screenshot)
parent.add_widget(self.painter)
parent.add_widget(ssbtn)
return parent
def save_screenshot(self,obj):
self.painter.export_to_png("screenshot.png")
MyPaintApp().run()

Issues with changing color of basic Kivy app

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.

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

How to make a carousel in Kivy?

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)

Categories

Resources