user8912366
user8912366

Reputation:

Kivy : Run .py file onclick on image

demo.Kv

AsyncImage:
            canvas:
                Rectangle:
                    texture: CoreImage("add.jpg").texture
                    size: self.width, self.height
                    size: 30, 30
                    pos: self.x - 1, self.y
                    #on_release: os.system("python test.py")

Can anyone help me?

 1. This code is in my .kv file. How to run test.py file onclick on add.jpg image.

Any advise or guidance would be greatly appreciated..!!

Upvotes: 0

Views: 620

Answers (2)

sp________
sp________

Reputation: 2645

You have to make a custom widget:

.py:

from kivy.uix.image import AsyncImage
import os


MyAsyncImage(AsyncImage)

    #other stuff

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            os.system('python file.py')

Then in your .kv:

MyAsyncImage:
    canvas:
        Rectangle:
            texture: CoreImage("add.jpg").texture
            size: self.width, self.height
            size: 30, 30
            pos: self.x - 1, self.y

Upvotes: 0

user4612771
user4612771

Reputation:

so somewhere relevant in your main.py probably the root(class) file write a function that will run the test.py like this

def run_test():
    os.system('python file.py')

and do import the os module. Now replace this with what you have in your kv file

on_release: root.run_test()

Upvotes: 1

Related Questions