【问题标题】:How do I load an image into a kivy window using a button?如何使用按钮将图像加载到 kivy 窗口中?
【发布时间】:2021-05-24 16:06:06
【问题描述】:

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

我目前在我的 .kv 文件中有单独插入按钮和图像的示例,但我不确定如何连接它们:

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}

我觉得我需要对 on_press 声明做点什么,但我不太确定是什么。任何帮助表示赞赏。

【问题讨论】:

    标签: python user-interface kivy kivy-language


    【解决方案1】:

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

    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)
    

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

    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)
    

    【讨论】:

    • 所以我的主要想法是使用滑块设置为特定值,然后有一个提交按钮,然后根据我使用滑块设置的值在应用程序中显示图像(图像文件名包含滑块的值)。我将如何将您的解决方案扩展到此?
    • 我已按照您的要求编辑了原始答案以扩展解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2015-01-05
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2021-04-20
    相关资源
    最近更新 更多