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.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
Builder.load_string("""
<ImageButton>:
FloatLayout:
Image:
source:'resizedA.png'
size_hint: .2, .2
""")
class ImageButton(ButtonBehavior,FloatLayout, Image):
def on_press(self):
print ('pressed')
class The_AssignmentApp(App):
def build(self):
return ImageButton()
if __name__ == "__main__":
The_AssignmentApp().run()
My question is, why is that even if I press the other parts of the screen(not the image), the app still considers the whole scree as a button?
Pardon my ignorance here, I would really want to learn. Thanks!
The default texture is grey, so just setting the background color will give a darker result. To set a plain color, set the background_normal to '' . New in version 1.0.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With