2014-11-03 57 views
3

我试图用Kivy来显示背景图片,所以我想让根部件与图片大小相同。目前,当我从kv文件加载图像时,它显示为左下角的小缩略图。应用程序窗口似乎是正确的(全尺寸)大小,但很难说。下面的代码,有什么想法?图片在基维的尺寸是错误的

.kv文件:

#:kivy 1.8.0 

<BarBot_splash>: 
    BoxLayout: 
     size: image.size 

     Image: 
      id: image 
      source: 'MainMenu.png' 

.py文件:

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() 

回答

1

在main.py:

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 

class BarBot_splash(BoxLayout): # subclass BoxLayout.. now it's inherently a Boxlayout 

KV:

#:kivy 1.8.0 

<BarBot_splash>: # I'm a BoxLayout... 
    Image: 
     id: image 
     source: 'MainMenu.png' 

这应该做的伎俩。 BoxLayout应该占据窗口。这是一个孩子,Image,应该反过来占据BoxLayout的全尺寸。为什么?那么,如果我错了,有人纠正我,但我认为这是因为BoxLayoutImagesize_hint属性默认为(1, 1),这意味着:“尽可能多地占用父母的空间”或(100 %宽度,100%高度)。虽然如果父母中还有其他孩子,如果孩子在BoxLayout中有几个Image,或者在您的应用中有多个BoxLayout等,则孩子可能无法占用其父母的所有区域。将size_hint设置为(.5, .3)意味着占用您父/容器的可用空间(宽度的50%,高度的30%)或可用空间。

2

BarBot_splash仅仅是一个小部件,因此它并不适用于它的任何儿童的位置或大小,因此BoxLayout的(并因此其子图像)仅具有(0,0)的默认位置和(100,100)的大小。

将BarBot_splash更改为BoxLayout或其他调整大小的布局,这样会正确传播。你也不需要size: image.size行,这没有任何作用。