Adding a icon image to a button with a custom background image?

2,356 views
Skip to first unread message

Ryan Hasse

unread,
Sep 13, 2012, 11:30:33 AM9/13/12
to kivy-...@googlegroups.com
I've already got a button with a custom up and pressed state images, but I'd like to be able to add an icon to it as well without having to make an whole new image for each button state each time (I'm stepping through a list of iterations to set up my buttons and I want to be able to change the icons on each of them at any time).

Thomas Hansen

unread,
Sep 13, 2012, 11:58:41 AM9/13/12
to kivy-...@googlegroups.com
You can just add another KV rule to the Button class, or a subclass
you create. here is an example https://gist.github.com/3715307

--
Thomas
> --
>
>
>

Gabriel Pettier

unread,
Sep 13, 2012, 12:06:05 PM9/13/12
to kivy-...@googlegroups.com

You could add the image as a child of the Button widgets. Or subclass the Button class to add an 'icon' ObjectProperty and adapt the text placement for the icon size if you want both.

Le 13 sept. 2012 17:30, "Ryan Hasse" <ryan...@gmail.com> a écrit :
I've already got a button with a custom up and pressed state images, but I'd like to be able to add an icon to it as well without having to make an whole new image for each button state each time (I'm stepping through a list of iterations to set up my buttons and I want to be able to change the icons on each of them at any time).

--
 
 
 

Ryan Hasse

unread,
Sep 13, 2012, 12:38:43 PM9/13/12
to kivy-...@googlegroups.com
Thank you. The subclass method works, but I have to put the class inside of my iterative for it to work and I don't know how good that is? It feels like that should be bad juju, but I don't know if it is in Python.

I tried having the images be children to the buttons, but they don't seem to adhere to the button's positioning that way, at all.

Ryan Hasse

unread,
Sep 13, 2012, 1:41:21 PM9/13/12
to kivy-...@googlegroups.com
Seems that if I do anything other than self.center for the position then the icons all orient based on the window instead of the button

Thomas Hansen

unread,
Sep 13, 2012, 1:58:23 PM9/13/12
to kivy-...@googlegroups.com
not sure what you mean by using the class inside your iterative?!
> --
>
>
>

Thomas Hansen

unread,
Sep 13, 2012, 2:01:14 PM9/13/12
to kivy-...@googlegroups.com
yes, if you give a integer it will be based on current transformation,
which is usually the window space (unless your using matrix
transformations or are inside e.g. a scatter or something simmilar).
but you can just use the center or pos attribute no?

e.g., if you want the icon to the left of the button, be 50px wide,
and as tall as the widget:

Rectangle:
source: self.icon
pos: self.x - 50, self.y
size: 50, self.height
> --
>
>
>

Ryan Hasse

unread,
Sep 13, 2012, 2:02:00 PM9/13/12
to kivy-...@googlegroups.com
    def BuildLayout(self):
        layout = GridLayout( rows=4, row_force_default = True, row_default_height = 100, col_force_default = True, col_default_width = 300 )

        with open('config.txt', 'rb') as f:
            reader = csv.reader(f, delimiter="|")
            for row in reader:

                class IconButton(Button):
                    icon = StringProperty(row[2])

                launchbutton = IconButton( text = row[0], background_normal = 'tile.png' )
                launchbutton.bind( on_release = lambda widget, appname=row[1]: self.Launcher( appname ) )
                layout.add_widget(launchbutton)

        return layout

That's what I ended up with to get it to work.

Ryan Hasse

unread,
Sep 13, 2012, 2:36:27 PM9/13/12
to kivy-...@googlegroups.com
I'm just not sure how to have it interact with `row[2]` in this case when defining at the module level. I don't seem to be able to do something like:
IconButton(Button, blah):
   icon
= blah
to pass the class arguments, it just tells me that blah is undefined instead.

On Thursday, September 13, 2012 11:03:51 AM UTC-7, Thomas Hansen wrote:
works I guess...but you dont have to declare the IconButton inside the
loop.  you can just define it at module level if you want...otherwise
your technically redefining the class each time through the loop.
> --
>
>
>

Ryan Hasse

unread,
Sep 13, 2012, 2:37:06 PM9/13/12
to kivy-...@googlegroups.com
That works, thanks!

Thomas Hansen

unread,
Sep 13, 2012, 2:03:51 PM9/13/12
to kivy-...@googlegroups.com
works I guess...but you dont have to declare the IconButton inside the
loop. you can just define it at module level if you want...otherwise
your technically redefining the class each time through the loop.

> --
>
>
>
Reply all
Reply to author
Forward
0 new messages