Image is wrong size in Kivy
I am trying to use Kivy to display a background image, so I want the root widget to be the same size as the image. Currently, when I load an image from a kv file, it shows up as a small thumbnail in the bottom left corner. The application window looks like the correct (full-blown) size, but it's hard to tell. Code below, any thoughts?
.kv file:
#:kivy 1.8.0
<BarBot_splash>:
BoxLayout:
size: image.size
Image:
id: image
source: 'MainMenu.png'
.py file:
import kivy
kivy.require('1.8.0')
from kivy.app import App
from kivy.uix.widget import Widget
class BarBot_splash(Widget):
def on_touch_up(self, touch):
if touch.x < self.width/3:
#we touched the menu button
pass
elif touch.x > self.width/3*2:
#we touched the custom button
pass
else:
return False
class BarBot(App):
def build(self):
return BarBot_splash()
if __name__=='__main__':
BarBot().run()
source to share
In main.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class BarBot_splash(BoxLayout): # subclass BoxLayout.. now it inherently a Boxlayout
kilovolt:
#:kivy 1.8.0
<BarBot_splash>: # I'm a BoxLayout...
Image:
id: image
source: 'MainMenu.png'
This should do the trick. BoxLayout
should occupy the window. One child, Image
in turn, must take full size BoxLayout
. What for? Well, someone correct me if I'm wrong, but I think because the property size_hint
for BoxLayout
and Image
defaults to (1, 1)
, which means "Take as much space in yours as you can" or (100% width, 100% height). Although a child will not be able to tackle the entire parent area if there are other children in the parent, for example if you had several Image
in BoxLayout
or more than one BoxLayout
in your application, etc. Setting the size_hint parameter to (.5, .3)
means occupying (50% width, 30% height) of your parent / container, or available space.
source to share
BarBot_splash is just a widget, so it doesn't apply any position or size to its children, so the boxlayout (and therefore its child image) only has a default position (0, 0) and size (100, 100).
Change BarBot_splash to BoxLayout or other sizing layout and this will spread correctly. You don't need a string size: image.size
either, it doesn't do anything.
source to share