Forum Archive

How do I add sprites and label nodes

sodoku

How do I add SpriteNodes and label nodes to the different coloured views

import ui
class TabbedView(ui.View):
    def __init__(self,tablist=[], frame=(0,0)+ui.get_screen_size()):
        #takes an iterable of Views, using the view name as the tab selector.  
        #empty views sre just given generic names
        self.tabcounter=0    #unique counter, for name disambiguation
        self.buttonheight=30 #height of buttonbar
        #setup button bar
        self.tabbuttons=ui.SegmentedControl(frame=(0,0,self.width, self.buttonheight))
        self.tabbuttons.action=self.tab_action
        self.tabbuttons.flex='W'
        self.tabbuttons.segments=[]
        self.add_subview(self.tabbuttons)

        for tab in tablist:
            self.addtab(tab)
    def tab_action(self,sender):
        if sender.selected_index >= 0:
            tabname=sender.segments[sender.selected_index]
            self[tabname].bring_to_front()
    def focus_tab_by_index(self,index):
        self.tabbuttons.selected_index=index
        self.tab_action(self.tabbuttons)

    def focus_tab_by_name(self,tabname):
        self.tabbuttons.selected_index=self.tabbuttons.segments.index(tabname)
        self.tab_action(self.tabbuttons)

    def addtab(self,tab):
            if not tab.name:
                tab.name='tab{}'.format(self.tabcounter)
            if tab.name in self.tabbuttons.segments:
                #append unique counter to name
                tab.name+=str(self.tabcounter)
            self.tabcounter+=1
            self.tabbuttons.segments+=(tab.name,) 
            tab.frame=(0,self.buttonheight,self.width,self.height-self.buttonheight)
            tab.flex='WH'
            self.add_subview(tab)
            self.focus_tab_by_name(tab.name)

    def removetab(self,tabname):
        self.tabbuttons.segments=[x for x in self.tabbuttons.segments if x != tabname]
        self.remove_subview(tabname)
        # if tab was top tab, think about updating selected tab to whatever is on top 

    def layout(self):
        pass   # maybe set tabbuttons size

if __name__=='__main__':
    v=TabbedView()
    v.addtab(ui.View(name='card1',bg_color='red'))
    v.addtab(ui.View(name= 'card3',bg_color='blue'))
    v.addtab(ui.View(name='card2',bg_color='green'))
    v.addtab(ui.View(name= 'card4',bg_color='#ff78be'))
    v.addtab(ui.View(name= 'mycard',bg_color='#f7ff43'))
    v.present()
ccc

Let's start hacking with f-strings...
tab.name='tab{}'.format(self.tabcounter) --> tab.name=f'tab{self.tabcounter}'

mikael

@sodoku, this is a ui view and you want to add scene Nodes. From the docs, you can ”create a SceneView explicitly, set its scene attribute to the scene you want to present, and then add it to a view hierarchy that you created using the ui module.”

cvp

@sodoku at begin of script

from   scene import *

class MyScene(Scene):
    def setup(self):
        label = LabelNode('this is a LabelNode')
        label.color = 'black'
        label.position = self.size/2
        self.add_child(label)

at end of add_tab def

            # tab is an ui.View
            tab.scene_view = SceneView(frame=(0,0,tab.width,tab.height))
            tab.scene_view.flex = 'WH'
            tab.scene_view.scene = MyScene()
            tab.scene_view.scene.background_color = tab.bg_color
            tab.add_subview(tab.scene_view)
sodoku

@cvp So could ? this be an alternative for scene selecting ,as opposed to presenting scene and dismissing scene