2016-02-08 23 views
0

我正在研究一個應用程序,我想在垂直BoxLayout中添加n個圖像幀。 n幀的數量取決於桌面屏幕的大小。在這之後,幀將被動態地填充圖像。我能夠以固定數量的幀以靜態方式進行開發。 現在我試圖將其轉換爲動態解決方案,其中將根據屏幕的高度創建n個框架小部件(在下面的示例中爲7個小部件)。我很失落..... :(使用python和.kv文件將靜態小部件樹轉換爲動態

該框架應頂杆和底杆之間「坐」在.kv文件,這是由#ILIST表示:。在線

我有以下問題:

1)如何使用.kv文件動態添加這些小部件?

2)我怎樣才能引用這些獨立的框架小部件來動態分配圖像?例如:frame [idx] = swid?

非常感謝您的時間和事先分享的知識。

Python的文件pplay.py:

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.uix.boxlayout import BoxLayout 
from kivy.core.window import Window 
from kivy.properties import StringProperty 

class IListItem(Widget): 
    sid = StringProperty('') 
    image = StringProperty('') 
    label = StringProperty('') 
    pass 

class IList(Widget): 
    pass 

class pplayHome(BoxLayout): 

    def init_player(self): 
     global swid 

     self.Ilayout = IList() 

     for idx in range (1, 7): 
      swid = IListItem(
        sid = "s" + str(idx), 
        image = 'empty_image.png', 
        label = 'Image' + str(idx) 
      ) 
      self.Ilayout.add_widget(swid) 

class pplayApp(App): 
    def build(self): 
     Window.clearcolor = (1,1,1,1) 
     Window.borderless = True 
     Window.size = 275, 1080 
     homeWin = pplayHome() 
     homeWin.init_player() 
     return homeWin 

if __name__ == "__main__": 
    pplayApp().run() 

和Kivy文件pplay.kv

# File: pplay.kv 

<[email protected]> 
    pid: self.pid 
    source: self.source 
    label: self.label 
    size_hint_y: None 
    height: "120dp" 

    BoxLayout: 
     orientation: "vertical" 
     Label: 
      size_hint: None, 1 
      text: root.label 

     Image: 
      size_hint: None, 1 
      source: root.source 

<pplayHome>: 
    orientation: "vertical" 
    ActionBar: 
     font_size: 8 
     size: (275,25) 
     # background color in Kivy acts as a tint and not just a solid color. 
     # set a pure white background image first. 
     background_image: 'white-bg.png' 
     background_color: 0,.19,.34,1 
     ActionView: 
      ActionPrevious: 
       with_previous: False 
       app_icon: 'trButton.png' 
      ActionOverflow: 
      ActionButton: 
       icon: 'butt_exit.png' 
       on_press: root.exit_player() 

# Ilist: 


    BoxLayout: 
     height: "10dp" 
     size_hint_y: None 
     pos_x: 0 
     canvas.before: 
      Color: 
       rgb: 0.55,0.77,0.25  # groen 
      Rectangle: 
       pos: self.pos 
       size: self.size 

回答

0

你實際上是相當接近。 這裏是稍微改變kv文件,添加了彩色矩形的大小說明:

# File: pplay.kv 

<[email protected]> 
    source: '' 
    label: '' 
    size_hint_y: None 
    height: "120dp" 
    canvas.before: 
     Color: 
      rgb: 0.55,0.77*self.idx/7,0.25  # groen 
     Rectangle: 
      pos: self.pos 
      size: self.size 
    BoxLayout: 
     orientation: "vertical" 
     size: root.size 
     pos: root.pos 

     Label: 
      size_hint: None, 1 
      text: root.label 
      canvas.before: 
       Color: 
        rgb: 0.77*root.idx/7,0.55,0.25  # groen 
       Rectangle: 
        pos: self.pos 
        size: self.size 
     Image: 
      size_hint: None, 1 
      source: root.source 

<pplayHome>: 
    orientation: "vertical" 
    ActionBar: 
     font_size: 8 
     size: (275,25) 
     # background color in Kivy acts as a tint and not just a solid color. 
     # set a pure white background image first. 
     background_image: 'white-bg.png' 
     background_color: 0,.19,.34,1 
     ActionView: 
      ActionPrevious: 
       with_previous: False 
       app_icon: 'trButton.png' 
      ActionOverflow: 
      ActionButton: 
       icon: 'butt_exit.png' 
       on_press: root.exit_player() 

    IList: 
     id: ilist 
     orientation: 'vertical' 
    BoxLayout: 
     height: "10dp" 
     size_hint_y: None 
     pos_x: 0 
     canvas.before: 
      Color: 
       rgb: 0.55,0.77,0.25  # groen 
      Rectangle: 
       pos: self.pos 
       size: self.size 

pplay.py,重要部分是檢索使用self.ids['ilist']IList部件。它還顯示了它的孩子們(IListItems)如何通過差異化的財產獲取。

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.uix.boxlayout import BoxLayout 
from kivy.core.window import Window 
from kivy.properties import StringProperty, NumericProperty 

class IListItem(Widget): 
    idx = NumericProperty(1) 
    sid = StringProperty('') 
    image = StringProperty('') 
    label = StringProperty('') 
    pass 

class IList(BoxLayout): 
    pass 

class pplayHome(BoxLayout): 

    def init_player(self): 
     #global swid 

     print self.ids 
     ilayout = self.ids['ilist'] 

     for idx in range (0, 7): 
      swid = IListItem(
        sid = "s" + str(idx), 
        image = 'empty_image.png', 
        label = 'Image' + str(idx), 
        idx=idx 
      ) 
      ilayout.add_widget(swid) 
     children_ids = [il.sid for il in ilayout.children] 
     print children_ids 
     print ilayout.children[children_ids.index('s3')] 

class pplayApp(App): 
    def build(self): 
     # Window.clearcolor = (1,1,1,1) 
     Window.borderless = True 
     Window.size = 275, 1080 
     homeWin = pplayHome() 
     homeWin.init_player() 
     return homeWin 

if __name__ == "__main__": 
    pplayApp().run() 
+0

嗨,這正是我正在尋找的。大。非常感謝您的快速幫助和答覆。高度讚賞。 –

+0

親愛的'zeeMonkeez',我正在進一步開發你的偉大代碼並且遇到以下內容。我想從我的python模塊訪問對象的'label'和'image'。因此,我正在使用以下代碼,但它幾乎不可讀:ilayout.children [children_ids.index('s3')]。label ='Hello World!'。我期待更多類似這樣的東西:self.ilist.s3.label,但很明顯,這是行不通的。是否有更短的格式來引用動態生成的小部件中的「標籤」? –

相關問題