Python3 and kivy dynamically resizing image when resizing kivy window

12 Views Asked by At
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.graphics import Rectangle
from kivy.uix.label import Label
from kivy.uix.button import Button

class BackgroundBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        kwargs['spacing'] = 0  # Remove spacing between children
        super().__init__(**kwargs)
        with self.canvas.before:
            self.rect = Rectangle(source='assets/canvas.png', pos=self.pos, size=self.size)
        self.bind(pos=self.update_rect, size=self.update_rect)

    def update_rect(self, instance, value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size

class FourBoxLayoutApp(App):
    def on_start_button_release(self, instance):
        print("Start button was pressed!")

    def build(self):
        # Set the main application window size
        Window.size = (320, 460)  # width, height
        
        # Create the root layout
        root_layout = BackgroundBoxLayout(orientation='vertical', spacing=0)  # Set spacing to 0

        # Create four BoxLayout widgets
        grid_layout = BoxLayout(orientation='horizontal', size_hint=(1, 0.7))

        dummy_box1 = BoxLayout(orientation='horizontal', size_hint=(1, 0.05))

        start_button_box = BoxLayout(orientation='vertical', size_hint=(1, 0.15))        
        start_button = Button(background_normal='assets/start.png', background_down='assets/start.png', size_hint=(None, None))
        start_button.pos_hint = {'center_x': 0.5}
        
        start_button.bind(on_release=self.on_start_button_release)
        start_button_box.add_widget(start_button)

        dummy_box2 = BoxLayout(orientation='horizontal', size_hint=(1, 0.05))
        # Add the box layouts to the root layout
        root_layout.add_widget(grid_layout)
        root_layout.add_widget(dummy_box1)
        root_layout.add_widget(start_button_box)
        root_layout.add_widget(dummy_box2)

        return root_layout

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

**Specifically dealing with here (I think):**

class BackgroundBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        kwargs['spacing'] = 0  # Remove spacing between children
        super().__init__(**kwargs)
        with self.canvas.before:
            self.rect = Rectangle(source='assets/canvas.png', pos=self.pos, size=self.size)
        self.bind(pos=self.update_rect, size=self.update_rect)

**and:**

        start_button_box = BoxLayout(orientation='vertical', size_hint=(1, 0.15))        
        start_button = Button(background_normal='assets/start.png', background_down='assets/start.png', size_hint=(None, None))
        start_button.pos_hint = {'center_x': 0.5}

I have done various things playing around with 'size_hint=(X, X)' and 'self.rect = Rectangle', but nothing seems to work. Trying to get image to resize on kivy window resize. Once window starts getting to certain smaller size the image maintains original size and overuns 'dummy_box1' and 'grid_box'. While enlarging image retains original size and does not scale with rest of canvas. Only post similar is quite vague and not sure appropriate.

0

There are 0 best solutions below