pythonimagekivywindow

Simple Window App - Not possible to set Image to correct size in Layout / Window


I am new to kivy, but as an experienced programmer absolutely struggeling. Asked chatgpt but outcoming app and results do not work either.

Totally simple: I want to show a PNG as background in a fixed size window. Image is guaranteed 1280x800.

This is the code, I left the comments in of my various tries, so you can see what I have done.

Any help welcome, I have no idea left and looked dozens of tutorials and articles, nothing works.

from kivy.config import Config
Config.set('graphics', 'resizable', '0')
Config.set('graphics', 'width', '1280')
Config.set('graphics', 'height', '800')
Config.set('graphics', 'left', 0)
Config.set('graphics', 'top',  0)

from kivy.app import App

from kivy.uix.image import Image
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout

class ImageApp(App):
    def build(self):
        #Window.size = (1280, 800)
        #Window.borderless = False #True
        Window.clearcolor = (0, 1, 0, 1)
        #Window.size_hint=(1, 1)

        #layout = FloatLayout(size=(1280, 800),size_hint=(None, None),pos = (0, 0))
        layout = FloatLayout()
        #layout.size_hint=(1, 1)       
        #layout.size = (1280, 800)        
        #layout.pos = (0, 0)        
        #return layout
        
        #label = Label(text="X", x=0, y=0)
        #layout.add_widget(label)        
            
        #layout.size = (1280, 800)

        #layout.size_hint=(None, None)
        #img = Image(source='testimage.png', size_hint=(None, None), allow_stretch=False, keep_ratio=True)
        #img = Image(source='testimage.png', size_hint=(None, None))
        img = Image(source='testimage.png')

        # does not work:
        #img.size_hint = (1, 1) 
        #img.pos_hint = {'x': 0, 'y': 0}  

        # does not work either:
        #img.size = (1280, 800)        
        #img.pos = (0, 0)
        
        layout.add_widget(img)
        return layout

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

And so it looks: App Window - Image not sized correctly


Solution

  • I didn't find anything wrong in the code. I ran the code with 2 .png images.

    One with 800x500 and it didn't fill the window as expected, then I tried another one with 1280x800 and it worked.

    You should check the dimensions of your image.

    NOTE: I uncommented

    Window.size = (1280, 800)

    layout.size = (1280, 800)