How to resize a image inside a button in kivy?

1,266 views
Skip to first unread message

Stuart

unread,
Dec 28, 2020, 12:13:25 AM12/28/20
to Kivy users support
Hi all

I am new to Kivy, can anyone help me with this ?


I am trying to use kivy and scrollview. I need to stop the button background image from stretching?

I am using background_normal to add a image to the button, How do I scale the image and stop it from stretching?

.kv

Button:
                    background_normal: 'teaflav/Crushes.png'
                    height: 40


any help would be good, Thank you :)

Elliot Garbus

unread,
Dec 28, 2020, 12:35:53 AM12/28/20
to kivy-...@googlegroups.com

Don’t use a button.

Combine ButtonBehavior and Image to create an Image button.  Then you can use the image attributes,  keep_ratio, allow stretch to control the image.

 

See:

https://kivy.org/doc/stable-1.11.1/api-kivy.uix.behaviors.button.html?highlight=buttonbehavior#example

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/a3d62f63-f064-4896-b6e2-fb9f5926f718o%40googlegroups.com.

 

Stuart

unread,
Dec 28, 2020, 1:08:44 AM12/28/20
to Kivy users support
Thanks for your help,

As you can see i am using screenmanager...I cant seem to use screenmanager and scrollview together and get a image on the button unless i use button ?

or is there a better way?

Thanks

On Monday, 28 December 2020 16:35:53 UTC+11, ElliotG wrote:

Don’t use a button.

Combine ButtonBehavior and Image to create an Image button.  Then you can use the image attributes,  keep_ratio, allow stretch to control the image.

 

See:

https://kivy.org/doc/stable-1.11.1/api-kivy.uix.behaviors.button.html?highlight=buttonbehavior#example

 

From: Stuart
Sent: Sunday, December 27, 2020 10:13 PM
To: Kivy users support
Subject: [kivy-users] How to resize a image inside a button in kivy?

 

Hi all

 

I am new to Kivy, can anyone help me with this ?

 

 

I am trying to use kivy and scrollview. I need to stop the button background image from stretching?

 

I am using background_normal to add a image to the button, How do I scale the image and stop it from stretching?

 

.kv

 

Button:
                    background_normal: 'teaflav/Crushes.png'
                    height: 40

 

 

any help would be good, Thank you :)

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Dec 28, 2020, 1:42:26 AM12/28/20
to kivy-...@googlegroups.com
You can use an image button in place of a button. This will work on screens and with ScrollView. I’ll take a look tomorrow morning. 

Sent from my iPhone

On Dec 27, 2020, at 11:08 PM, Stuart <snor...@gmail.com> wrote:


To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/466c3cb7-6334-4738-a6f2-e5983b1874c1o%40googlegroups.com.

Elliot Garbus

unread,
Dec 28, 2020, 9:33:05 AM12/28/20
to kivy-...@googlegroups.com

Here is a standalone running example.  I replaced your image with one I had, you will need to replace the image.

 

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image


kv =
"""
#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManager:
    transition: FadeTransition()
    MainScreen:
        name: "main"
    AnotherScreen:
        name: 'other'

<AnotherScreen@Screen>:
    Button:
        text: 'Return to Main'
        on_release: root.manager.current = 'main'
        

<MainScreen@Screen>:
    BoxLayout:
        ScrollView:
            GridLayout:
                id: container_y
                size_hint_y: None
                cols: 2
                row_default_height: root.height*0.3
                height: self.minimum_height
                Image:
                    source: "drink.png"
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
"""


class ImageButton(ButtonBehavior, Image):
   
pass


class
ImageButtonApp(App):
   
def build(self):
       
return Builder.load_string(kv)


ImageButtonApp().run()

Stuart

unread,
Dec 28, 2020, 7:08:53 PM12/28/20
to Kivy users support
> To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/466c3cb7-6334-4738-a6f2-e5983b1874c1o%40googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61A85B3B-04FD-4D91-B51A-8AA4C421F9A9%40cox.net.
>  

thanks...I am afk till after new years I will try it then...thank you so much!

Stuart

unread,
Dec 30, 2020, 9:42:21 PM12/30/20
to Kivy users support
Thank you this WORKS!!!
Reply all
Reply to author
Forward
0 new messages