converting static widget tree to dynamic using python and .kv file -
i working on application want add n image frames in vertical boxlayout. number of n frames depend on desktop screen size. next frames filled dynamically images. able develop in static manner fixed number of frames. trying convert dynamic solution n frame widgets created depending on height of screen (in example below 7 widgets). , lost..... :(
the frames should 'sit' between top bar , bottom bar. in .kv file indicated # ilist: line.
i have following questions:
1) how add these widgets dynamically using .kv file?
2) how reference these individual frame widgets assigning images dynamically? example: frame[idx] = swid?
thanks time , shared knowledge in advance.
the python file pplay.py:
from kivy.app import app kivy.uix.widget import widget kivy.uix.boxlayout import boxlayout kivy.core.window import window 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() 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() and kivy file pplay.kv
# file: pplay.kv <ilist@boxlayout> 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 tint , not solid color. # set 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
you pretty close. here changed kv file, added colored rectangles illustration of sizes:
# file: pplay.kv <ilistitem@widget> 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 tint , not solid color. # set 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 in pplay.py, essential part retrieve ilist widget using self.ids['ilist']. shows how children (the ilistitems) can retrieved via differentiating property.
from kivy.app import app kivy.uix.widget import widget kivy.uix.boxlayout import boxlayout kivy.core.window import window 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'] 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 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()
Comments
Post a Comment