Kivy— Image as Button

前端 未结 1 658
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 10:06

I am just beginner in kivy and object oriented programming.

I have been practicing this code as a combination of the tutorials here:

from kivy.uix.be         


        
相关标签:
1条回答
  • 2021-01-18 11:00
    class ImageButton(ButtonBehavior,FloatLayout, Image):  
    

    Don't inherit from multiple widgets (in this case FloatLayout and Image), this will lead to some weird bugs.

    As for your specific problem, the ButtonBehavior is a parent class of the ImageButton which is the root widget and fills the screen. For this reason, the whole screen is a button, though you clearly intended otherwise.

    Is the following more like what you wanted? You can also just use a FloatLayout instead of creating the new RootWidget class, I just did this to fit with what you already wrote.

    from kivy.uix.behaviors import ButtonBehavior  
    from kivy.uix.image import Image  
    from kivy.lang import Builder  
    from kivy.app import App  
    from kivy.uix.floatlayout import FloatLayout  
    
    class RootWidget(FloatLayout):
        pass
    
    class ImageButton(ButtonBehavior, Image):  
        def on_press(self):  
            print ('pressed')
    
    Builder.load_string("""  
    <RootWidget>:  
        ImageButton:  
            source:'resizedA.png'  
            size_hint: .2, .2  
    """)  
    
    class The_AssignmentApp(App):  
        def build(self):  
            return RootWidget()
    
    if __name__ == "__main__":  
        The_AssignmentApp().run()  
    
    0 讨论(0)
提交回复
热议问题