omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    How do I add sprites and label nodes

    Pythonista
    4
    5
    1933
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • sodoku
      sodoku last edited by ccc

      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()
      
      mikael cvp 2 Replies Last reply Reply Quote 0
      • ccc
        ccc last edited by

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

        1 Reply Last reply Reply Quote 0
        • mikael
          mikael @sodoku last edited by

          @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.”

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp @sodoku last edited by 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 1 Reply Last reply Reply Quote 0
            • sodoku
              sodoku @cvp last edited by sodoku

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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors