Understanding of to_local, to_window...

24 views
Skip to first unread message

Frédéric

unread,
Mar 24, 2023, 8:38:33 AM3/24/23
to Kivy users support
Hello,

I'm trying to get the coordinates of an image inside a scatter, in "global" value

I thought that I had to convert with to_window, but it does not work, the way I use it.
Here is a .kv file I used to test, an I don't understand the result

-------------------------------
<Label>:
    background_color: (0, 0, 1, 1)
    canvas.before:
        Color:
            rgba: self.background_color
        Rectangle:
            size: self.size
            pos: self.pos
       

<Screen>:
    id: baseLayout
    FloatLayout:
        size_hint: 1, 1
        canvas.before:
            Color:
                rgba: (0,0,0,1)
            Rectangle:
                size: self.size
                pos: self.pos
            Color:
                rgba: (1,0,1,1)
            Line:
                width: 2
                rectangle: (5, 5, self.width -10, self.height - 10)
               
        Scatter:
            id: scatter
            size_hint: None, None
            size: image.size
            scale_min: 0.5
            scale_max: 2
            auto_bring_to_front: False
            pos: root.center_x - self.width/2, root.center_y - self.height/2
           
            Image:
                id: image
                source: 'Mésange Charbonière.jpg'
                size: self.texture_size
           
        Label:
            pos_hint:{"x":0.7,"y":0.9}
            size_hint: 0.3, 0.1
            text: 'Size : ' + str(scatter.size)
        Label:
            pos_hint:{"x":0.7,"y":0.8}
            size_hint: 0.3, 0.1
            text: 'bbox : ' + str(scatter.bbox)
        Label:
            pos_hint:{"x":0.7,"y":0.7}
            size_hint: 0.3, 0.1
            text: 'image pos : ' + str(image.pos)
        Label:
            pos_hint:{"x":0.7,"y":0.6}
            size_hint: 0.3, 0.1
            text: 'image size : ' + str(image.size)
        Label:
            pos_hint:{"x":0.7,"y":0.5}
            size_hint: 0.3, 0.1
            text: 'image pos to_window : ' + str(image.to_window(*image.pos))
        Label:
            pos_hint:{"x":0.7,"y":0.4}
            size_hint: 0.3, 0.1
            text: 'image size to_window: ' + str(image.to_window(*image.size))
        Label:
            pos_hint:{"x":0.7,"y":0.3}
            size_hint: 0.3, 0.1
            text: 'image pos to_local : ' + str(image.to_local(*image.pos))
        Label:
            pos_hint:{"x":0.7,"y":0.2}
            size_hint: 0.3, 0.1
            text: 'image pos to_parent : ' + str(image.to_parent(*image.pos))
        Label:
            pos_hint:{"x":0.7,"y":0.1}
            size_hint: 0.3, 0.1
            text: 'image pos to_widget : ' + str(image.to_widget(*image.pos))

--------------------------------

The image is inside a scatter, itself inside a floatLayout, so the pos and size coordinates of the image are related to the scatter.

I belived that some of the coordinate translation would give something else than pos(0, 0) and size: (image_size), but it is always the same, even after sizing and rotating the scatter

Where is my misunderstanding?

Frederic

Elliot Garbus

unread,
Mar 24, 2023, 11:23:51 AM3/24/23
to kivy-...@googlegroups.com

Read: https://kivy.org/doc/stable/api-kivy.uix.scatter.html?highlight=scatter#module-kivy.uix.scatter

 

I suspect what you want is the bbox.   See: https://kivy.org/doc/stable/api-kivy.uix.scatter.html?highlight=scatter#kivy.uix.scatter.Scatter.bbox

 

There is an example of a scatter in the kivy-examples directory: venv/share/kivy-examples/widgets/scatter.py, and scatter.kv

It provides an interactive display of the scatter parameters.

--
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/d872b1af-9f29-4b06-818e-8858cefc2161n%40googlegroups.com.

 

Siva 14

unread,
Mar 24, 2023, 11:54:30 AM3/24/23
to kivy-...@googlegroups.com
Can anyone help me to change the below function into asynchronous

  def download(self):
     link = self.root.ids.ytbar.text
     clarity = self.root.ids.oop.text
     vdo = YouTube(link)
     Download = vdo.streams.filter(res = clarity).first() 
     Download.download()

If any one know pls help me with this
   
   

Elliot Garbus

unread,
Mar 24, 2023, 12:01:30 PM3/24/23
to kivy-...@googlegroups.com
The underlying function making the network call in the library is not using async.  Putting an async wrapper around it will not deliver the desired result. 

Did you try threading?

Sent from my iPhone

On Mar 24, 2023, at 8:54 AM, Siva 14 <sivau...@gmail.com> wrote:



Siva 14

unread,
Mar 24, 2023, 12:03:25 PM3/24/23
to kivy-...@googlegroups.com
Vro but i didn't know what to do with threading

Elliot Garbus

unread,
Mar 24, 2023, 5:33:48 PM3/24/23
to kivy-...@googlegroups.com

Here is an example.  I am using time.sleep to simulate the download.

 

from kivy.app import App
from kivy.lang import Builder
import time
from threading import Thread

kv =
"""
AnchorLayout:
    BoxLayout:
        size_hint: None, None
        size: 300, 48 * 2
        orientation: 'vertical'
        BoxLayout:
            Button:
                text: 'Not Threaded'
                on_release: app.not_threaded()
            Button:
                text: 'Threaded'
                on_release: app.threaded()
        Button:
            text: 'Test'
            on_release: print('Test Button Pressed')
"""

class ThreadSleepApp(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def threaded(self):
        t = Thread(
target=self.download)
        t.start() 
# start the download thread

   
def download(self): # simulating your download function...
       
print('starting the threaded download...')
        time.sleep(
10)
       
print('Download complete')

   
def not_threaded(self):
       
print('start non-threaded download')
        time.sleep(
10)
       
print('Finished non-threaded download')

ThreadSleepApp().run()

Siva 14

unread,
Mar 24, 2023, 9:13:51 PM3/24/23
to kivy-...@googlegroups.com
Will it work with my download method. Should i write time.sleep() for all my another methods

Elliot Garbus

unread,
Mar 24, 2023, 9:51:02 PM3/24/23
to kivy-...@googlegroups.com
Do not use time.sleep(). That is a place holder for long running code. Replace time.sleep() with your download code.  Look at how a thread is created and started. 

Sent from my iPhone

On Mar 24, 2023, at 6:13 PM, Siva 14 <sivau...@gmail.com> wrote:



Siva 14

unread,
Mar 26, 2023, 12:58:16 AM3/26/23
to kivy-...@googlegroups.com
def download(self):
        link = self.root.ids.ytbar.text
        clarity = self.root.ids.oop.text
        vdo = YouTube(link)
        Download = vdo.streams.filter(res=clarity).first()

        
        # Start the download in a separate thread
        self.download_thread = threading.Thread(target=self._download_, args=(Download,))
        self.download_thread.start()

    def _download_(self, download):
        download.download()
        self.progress_dialog.dismiss()

Still my app is strucking what should i do. Anyone reply this email pls


Elliot Garbus

unread,
Mar 26, 2023, 11:23:38 AM3/26/23
to kivy-...@googlegroups.com

Try something like this:

 


def _download(self): # this is your original download method, renamed to _download
   
link = self.root.ids.ytbar.text

    clarity = self.root.ids.oop.text
    vdo = YouTube(link)
    video = vdo.streams.filter(res=clarity).first()  # renamed Download to video
   
video.download()

def download(self):
    t = threading.Thread(
target=self._download)
    t.start()
   

 

I renamed your original method download to _download.  I created a new method download, that creates and starts the download thread.

In the _download method, I renamed Download to video.  In python the naming convention is that CapWords are used for Class definitions, see: https://pep8.org/#class-names , method and instance variables use snake_case.

 

I have not run this code, I am assuming your original download method worked as you intended.

 

Here is how to think about this conceptually.  A thread is another thread of execution.  It is running concurrently with other code in your program.  You create the thread by passing the callable (function or method) to the Thread class constructor.  Then call the start method on the thread object.  This code will run concurrently with the kivy event loop.  Threads all share the same address space.

 

Let me know if this helps.

Siva 14

unread,
Mar 26, 2023, 11:49:16 AM3/26/23
to kivy-...@googlegroups.com
Thanks for the reply bro but i already found that & fixed that but during the download of video the app becomes little laggy is it okay or is there any thing should i do

Elliot Garbus

unread,
Mar 26, 2023, 12:18:34 PM3/26/23
to kivy-...@googlegroups.com

If you want it to run faster, you could use multi-processing.  This adds a number of complexities.  If you decide to try it, I would recommend storing the video in a file,  it will be easier to share. 

 

With multi-processing, the download code would run on a separate CPU core.  This can improve performance but each process has it’s own address space, this make sharing data more complex.  Here is a simple multi-processing app I created with a few of the key learnings captured in the readme. 

https://github.com/ElliotGarbus/MidiClockGenerator

 

Pay special attention to: __name__ will get a different name in the process that is created by the program (__mp_main__). The test "if __name__ == '__main__':" prevents the kiy code from being loaded in both processes. If this 'guard' is not in place, the app will create 2 windows.

 

I don’t know enough about your app to have an opinion on if you should try this or not.  If you want to download a number of videos in the background, without impacting the UI, this would be the way to go.  If you are downloading one video at a time and waiting for the result, create a progress bar or other indication to the user that the file is downloading.

Reply all
Reply to author
Forward
0 new messages