【问题标题】:Kivy : Run .py file onclick on imageKivy:在图像上运行 .py 文件
【发布时间】:2017-11-17 07:35:16
【问题描述】:

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

谁能帮帮我?

1. 此代码在我的 .kv 文件中。如何在点击 add.jpg 图像时运行 test.py 文件。

任何建议或指导将不胜感激..!!

【问题讨论】:

    标签: python python-2.7 kivy kivy-language


    【解决方案1】:

    所以在你的 main.py 中某个相关的地方可能是根(类)文件编写了一个函数,它将像这样运行 test.py

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

    并导入 os 模块。现在将其替换为您的 kv 文件中的内容

    on_release: root.run_test()
    

    【讨论】:

    • 你需要一个自定义小部件,@sp-sp 在上面提供了一个很好的方法
    【解决方案2】:

    您必须制作一个自定义小部件:

    .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')
    

    然后在你的 .kv 中:

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

    【讨论】:

      猜你喜欢
      • 2016-03-18
      • 2021-04-03
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2021-03-31
      • 1970-01-01
      相关资源
      最近更新 更多