如何使用按钮将图像加载到Kivy窗口中? [英] How do I load an image into a kivy window using a button?

查看:77
本文介绍了如何使用按钮将图像加载到Kivy窗口中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用kivy与Python一起开发一个带有滑块的快速应用程序,其中必须先使用滑块来确定设置,然后单击提交",然后将所需的图像加载到应用程序窗口中.>

我目前在.kv文件中有一些示例,用于分别插入按钮和图像,但是我不确定如何连接它们:

  BoxLayout:方向:垂直"大小:root.width,root.heightfont_size:20内边距:100间距:10按钮:文字:按我"on_press:打印("ouch!请轻柔些")on_release:print("ahhh")on_state:#print(我当前的状态为{}".format(self.state))size_hint:(0.3,0.3)图像:来源:"images \ IMG_6980.jpg"#allow_stretch:正确#keep_ratio:正确pos_hint:{'center_x':0.7} 

我觉得我需要对on_press语句做些事情,但是我不太确定要做什么.感谢您的帮助.

解决方案

下面我为您提供了一个示例,该示例说明了如何使用您所描述的on_press方法从按钮生成图像.通过使用工厂模块,您可以生成在* .kv文件中创建的模板.因此,要完成程序,您将创建更多这些模板,然后使用条件句在on_press方法中生成适当的图像模板.您也可以尝试在Python中创建动态模板,但我相信我的示例会更简单.

test.py:

  import kivykivy.require('2.0.0')从kivy.app导入App从kivy.uix.boxlayout导入BoxLayout类TestBoxLayout(BoxLayout):经过TestApp(App)类:def构建(自己):返回TestBoxLayout()如果__name__ =='__main__':TestApp().run() 

test.kv:

 #:导入Factory kivy.factory.Factory< TestImage @ Image> ;:来源:"test.jpg"#allow_stretch:正确#keep_ratio:正确pos_hint:{'centre_X':0.7}< TestBoxLayout> ;:方向:垂直"大小:root.width,root.heightfont_size:20内边距:100间距:10按钮:文字:按我"on_press:root.add_widget(Factory.TestImage())on_release:print("ahhh")on_state:#print(我当前的状态为{}".format(self.state))size_hint:(0.3,0.3) 

为了将此解决方案扩展到您的评论,我在下面给出了另一个示例.我想重申一下,我的示例旨在易于理解,并且肯定可以更高效地完成,但是希望这可以为您的问题提供明确的解决方案.如果满足您的需求,请将此标记为最佳答案!

test.kv:

 #:导入Factory kivy.factory.Factory< TestImage1 @ Image> ;:来源:"test_1.jpg"#allow_stretch:正确#keep_ratio:正确pos_hint:{'centre_X':0.7}< TestImage2 @ Image> ;:来源:"test_2.jpg"#allow_stretch:正确#keep_ratio:正确pos_hint:{'centre_X':0.7}< TestBoxLayout> ;:方向:垂直"大小:root.width,root.heightfont_size:20内边距:100间距:10滑杆:id:滑块最小值:1最多:2步骤1按钮:文字:按我"on_press:如果slider.value == 1:root.add_widget(Factory.TestImage1())elif slide.value == 2:root.add_widget(Factory.TestImage2())on_release:print("ahhh")on_state:#print(我当前的状态为{}".format(self.state))size_hint:(0.3,0.3) 

I am trying to use kivy with Python to develop a quick app with sliders where one has to use the sliders first to determine a setting and then click a 'Submit' which then loads the desired image into the app window.

I currently have examples in my .kv file to insert a button and an image indiviudally, but I'm not sure how to connect them:

BoxLayout:
    orientation:'vertical'
    size: root.width,root.height
    font_size: 20
    padding: 100
    spacing: 10

    Button:
        text: 'press me'
        on_press: print("ouch! More gently please")
        on_release: print("ahhh")
        on_state:
        #print("my current state is {}".format(self.state))
        size_hint: (0.3,0.3)

    Image:
        source: 'images\IMG_6980.jpg'
        #allow_stretch: True
        #keep_ratio: True
        pos_hint: {'center_x':0.7}

I feel like I need to do something with the on_press statement but I'm not quite sure what. Any help is appreciated.

解决方案

I have given you an example below of how to generate an image from a button using the on_press method as you have described. By using the factory module, you can generate templates created in your *.kv files. So to complete your program, you would create more of these templates, and then generate the appropriate image template in your on_press method using conditionals. You could alternatively try to create dynamic templates within Python, but I believe my example to be more simple.

test.py:

import kivy
kivy.require('2.0.0')

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout


class TestBoxLayout(BoxLayout):
    pass


class TestApp(App):
    def build(self):
        return TestBoxLayout()


if __name__ == '__main__':
    TestApp().run()

test.kv:

#: import Factory kivy.factory.Factory


<TestImage@Image>:
    source: 'test.jpg'
    #allow_stretch: True
    #keep_ratio: True
    pos_hint: {'centre_X':0.7}


<TestBoxLayout>:
    orientation:'vertical'
    size: root.width,root.height
    font_size: 20
    padding: 100
    spacing: 10

    Button:
        text: 'press me'
        on_press: root.add_widget(Factory.TestImage())
        on_release: print("ahhh")
        on_state:
        #print("my current state is {}".format(self.state))
        size_hint: (0.3,0.3)

To extend this solution to your comment, I have given a further example below. I want to reiterate that my example is designed to be simple to understand and can certainly be done more efficiently, but hopefully this acts as a clear solution to your problem. Please mark this as the best answer if this fulfils your needs!

test.kv:

#: import Factory kivy.factory.Factory


<TestImage1@Image>:
    source: 'test_1.jpg'
    #allow_stretch: True
    #keep_ratio: True
    pos_hint: {'centre_X':0.7}


<TestImage2@Image>:
    source: 'test_2.jpg'
    #allow_stretch: True
    #keep_ratio: True
    pos_hint: {'centre_X':0.7}


<TestBoxLayout>:
    orientation:'vertical'
    size: root.width,root.height
    font_size: 20
    padding: 100
    spacing: 10

    Slider:
        id: slider
        min: 1
        max: 2
        step: 1

    Button:
        text: 'press me'
        on_press: 
            if slider.value == 1: root.add_widget(Factory.TestImage1())
            elif slider.value == 2: root.add_widget(Factory.TestImage2())
        on_release: print("ahhh")
        on_state:
        #print("my current state is {}".format(self.state))
        size_hint: (0.3,0.3)

这篇关于如何使用按钮将图像加载到Kivy窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆