Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

kivy Button to remain fixed to image even when resizing

26 views
Skip to first unread message

Steven Gerber

unread,
Mar 5, 2025, 10:57:36 AMMar 5
to Kivy users support
Hi all...I just started with Python & Kivy...

How does one get a button to remain fixed in a specific position on a centred image even when resizing screen...



For the life of me cannot figure it out...

My code below...
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen

Window.size = (450,750)
Code = """

ScreenManager:
    EzHomeScreen:

<EzHomeScreen>:
    name: 'EzHome'
    canvas.before:        
    BoxLayout:
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        size: 322, 680
        Image:
            source: 'Images\main.png'      

    Button:
        background_color: 1, 0 ,0, 0
        Image:
            source: 'Images\launch.png'
            pos: self.pos
       


"""
class EzHomeScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(EzHomeScreen(name='EzHome'))

class Tester(App):
    def build(self):
        scr = Builder.load_string(Code)
        return scr

if __name__ == '__main__':
    Tester().run()

ElliotG

unread,
Mar 5, 2025, 1:16:40 PMMar 5
to Kivy users support
There are a number of issues with the example code, details below.
There are a number of ways to center a Widget.  In a RelativeLayout or a FloatLayout, use a pos_hint.  A Screen is a ReleativeLayout.
You could also put the widget in an AnchorLayout.

If you want to set the size of a widget, you must "turn off" the size_hint by setting it to None.  If this is not done the size hint defaults to 1, 1 and the size is ignored.

Layouts are tools for positioning and sizing widgets.  In your example code you have put an Image in a Button.  This does not make sense.  If you want an image to behave like a button, you would combine ButtonBehavior with an Image  see: https://kivy.org/doc/stable/api-kivy.uix.behaviors.button.html#example

See the comments in the code.

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen

Window.size = (450, 750)
Code = """

ScreenManager:  # this is the root widget
    EzHomeScreen:

<EzHomeScreen>:
    name: 'EzHome'
    # canvas.before:  #<--- This does not make sense here.... no canvas instructions

    BoxLayout:
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        size_hint: None, None

        size: 322, 680
        Image:
            source: 'Images\main.png'      

    Button:
        # background_color: 1, 0 ,0, 0 # <- setting alpha to zero makes button invisible

        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        text: 'Button'
        size_hint: None, None
        size: 200, 48
    # Image:                           <--- Image was filling screen over button, put widgets in layouts
    #     source: 'Images\launch.png'
    #     pos: self.pos
"""


class EzHomeScreen(Screen):
    pass


# sm = ScreenManager()  #### This is redundant with the KV code.
# sm.add_widget(EzHomeScreen(name='EzHome'))



class Tester(App):
    def build(self):
        scr = Builder.load_string(Code)
        return scr


if __name__ == '__main__':
    Tester().run()

Steven Gerber

unread,
Apr 13, 2025, 10:03:39 AMApr 13
to Kivy users support
Only just saw this response now...Apologies Elliot...Spot on advice as always...
Thank You
Reply all
Reply to author
Forward
0 new messages