2017-04-16 112 views
0

我看着this的問題,但那是不同的。kivy:如何在BoxLayout中放置圖像?

我剛開始用kivy,所以我想這是很簡單的東西:\

我想兩個圖像放置到的BoxLayout。這是我到目前爲止的代碼:

import kivy 
kivy.require('1.9.1') 

from kivy.app import App 
from kivy.core.image import Image as CoreImage 
from kivy.uix.boxlayout import BoxLayout 

class TestApp(App): 

    def build(self): 
     layout = BoxLayout(orientation='horizontal') 

     im1 = CoreImage("image1.png") 
     im2 = CoreImage("image2.png") 

     layout.add_widget(im1) # this is line 33 
     layout.add_widget(im2) 

     return layout 

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

程序不顯示一個窗口,但打印此錯誤信息到終端:

Traceback (most recent call last): 
    File "./myprog.py", line 39, in <module> 
    TestApp().run() 
    File "/usr/lib/python3.6/site-packages/kivy/app.py", line 802, in run 
    root = self.build() 
    File "./myprog.py", line 33, in build 
    layout.add_widget(im1) 
    File "/usr/lib/python3.6/site-packages/kivy/uix/boxlayout.py", line 212, in add_widget 
    pos_hint=self._trigger_layout) 
    File "kivy/_event.pyx", line 438, in kivy._event.EventDispatcher.bind (kivy/_event.c:6473) 
KeyError: 'pos_hint' 

存在的圖像。如果我將pos_hint={'x':0,'y':0}添加到CoreImage「構造函數」中,我會得到相同的錯誤。

我正在使用python 3.6.0和kivy 1.9.1

回答

1

核心圖像不是小部件。您應該改用kivy.uix.image
kivy.core.image

核心類加載圖像並將其轉換爲紋理。原始圖像數據可以保存在內存中以便進一步訪問。

因此,爲了使您的工作例如:

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

class TestApp(App): 

    def build(self): 
     layout = BoxLayout(orientation='horizontal') 

     im1 = Image(source="image1.png") 
     im2 = Image(source="image2.png") 

     layout.add_widget(im1) 
     layout.add_widget(im2) 

     return layout 

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