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.


    Finding a more elegant way of communicating between views

    Pythonista
    4
    32
    10953
    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.
    • cvp
      cvp @superrune last edited by cvp

      @superrune a button action needs sender as parameter to identify which object has been tapped

              def openFileWindow(sender):
      
      1 Reply Last reply Reply Quote 0
      • superrune
        superrune last edited by superrune

        I’m slowly moving closer to something that works! So I moved the open view function out of the class, and now opening the window works! So now I need to get these two windows to talk to each other. Inside the init of the file window I am printing the superview to make sure it’s actually a child, but superview returns none. So why isn’t the parent/child relationship set up, even though I am creating this as a sub view?

        #!python3
        import ui
        from glob import glob
        from os.path import basename
        
        class fileWindow(ui.View):
        	def __init__(self):
        		self.frame=(100, 150, 300, 300)
        		self.name = 'File window'
        		self.border_width = 2
        		print ('Loader superview:', self.superview)
        		
        		imagefiles = [basename(x) for x in glob('*.*')]
        		
        		def loadAction(sender):
        			selectedFile = imagefiles[filelist.selected_row[1]]
        			print ('Selected ' + selectedFile + ' from sender: ' + sender.name)
        			# Sends the selected image to the pixel Editor
        			self.superview.text = selectedFile
        		
        		def fileSelected(sender):
        			# This does not work..
        			filePreview.background_color = 'red'
        		
        		filelistData = ui.ListDataSource(imagefiles)
        		filelistData.delete_enabled=False
        		
        		filelist = ui.TableView(frame=(10, 10 ,150, 280), data_source=filelistData, name='filelist')
        		filelist.row_height = 24
        		filelist.action = fileSelected # This does not work...
        		self.add_subview(filelist)
        		
        		filePreview = ui.ImageView(frame=(170,10,120,100))
        		filePreview.background_color = 'black'
        		self.add_subview(filePreview)
        		
        		loadButton = ui.Button(name='Load', frame=(170,120,64,32), title='Load')
        		loadButton.background_color = 'white'
        		loadButton.action = loadAction
        		self.add_subview(loadButton)
        		
        def openFileWindow(sender):
        		fv = fileWindow()
        		sender.add_subview(fv)
        		fv.present()
        		#fv = fileWindow()
        		#v.present()
        		print('File window opened.')
        
        class pixelEditor(ui.View):	
        	def __init__(self, width=640, height=480):
        		self.bg_color = 'grey'
        			
        		fileButton = ui.Button(name='File', frame=(10,80,64,32), title='File Window')
        		fileButton.background_color = 'white'
        		fileButton.action = openFileWindow
        		self.add_subview(fileButton)
        		
        		fileLabel = ui.Label(frame=(100, 80, 300, 32), font=('HelveticaNeue-Light', 32), text='___')
        		fileLabel.name = 'File Label'
        		self.add_subview(fileLabel)
        		
        		print(self.superview)
        	
        	
        v = pixelEditor()
        v.present('fullscreen')
        
        1 Reply Last reply Reply Quote 0
        • superrune
          superrune last edited by

          I tried changing self to sender in the function that opens the window, but still the parent view is returned as None.

          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @superrune last edited by cvp

            @superrune normal way is this, remark action = self.xxxxx and def xxx(self,sender) at same level as the def init

            
            class pixelEditor(ui.View): 
                def __init__(self, width=640, height=480):
                    self.bg_color = 'grey'
                        
                    fileButton = ui.Button(name='File', frame=(10,80,64,32), title='File Window')
                    fileButton.background_color = 'white'
                    fileButton.action = self.openFileWindow
                    self.add_subview(fileButton)
                    
                    fileLabel = ui.Label(frame=(100, 80, 300, 32), font=('HelveticaNeue-Light', 32), text='___')
                    fileLabel.name = 'File Label'
                    self.add_subview(fileLabel)
                    
                    print(self.superview)
                    
                def openFileWindow(self, sender):
                    fv = fileWindow()
                    fv.present()
                    print('File window opened.')```
            1 Reply Last reply Reply Quote 0
            • superrune
              superrune last edited by

              When I do that, I get the error message 'pixelEditor' object has no attribute 'openFileWindow'

              Moving the definition before the action assignment doesnt change anything either.

              cvp 2 Replies Last reply Reply Quote 0
              • cvp
                cvp @superrune last edited by cvp

                @superrune did you remark the new indentation of OpenFileWindow

                It became a method of the class...

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @superrune last edited by cvp

                  @superrune The other problem, of the superview, is that you use self.superview in the init.
                  The class is not yet really created. Try this and see where I use the self.superview

                  #!python3
                  import ui
                  from glob import glob
                  from os.path import basename
                  
                  class fileWindow(ui.View):
                      def __init__(self):
                          self.frame=(100, 150, 300, 300)
                          self.name = 'File window'
                          self.border_width = 2
                          #print ('Loader superview:', self.superview)
                          
                          imagefiles = [basename(x) for x in glob('*.*')]
                  
                          
                          filelistData = ui.ListDataSource(imagefiles)
                          filelistData.delete_enabled=False
                          
                          filelist = ui.TableView(frame=(10, 10 ,150, 280), data_source=filelistData, name='filelist')
                          filelist.row_height = 24
                          filelist.action = self.fileSelected # This does not work...
                          self.add_subview(filelist)
                          
                          filePreview = ui.ImageView(frame=(170,10,120,100))
                          filePreview.background_color = 'black'
                          self.add_subview(filePreview)
                          
                          loadButton = ui.Button(name='Load', frame=(170,120,64,32), title='Load')
                          loadButton.background_color = 'white'
                          loadButton.action = self.loadAction
                          self.add_subview(loadButton)
                          
                          ui.delay(self.x,0.01)
                      def x(self):
                          print ('Loader superview title = ', self.superview.title)
                          
                      def loadAction(self,sender):
                          selectedFile = imagefiles[filelist.selected_row[1]]
                          print ('Selected ' + selectedFile + ' from sender: ' + sender.name)
                          # Sends the selected image to the pixel Editor
                          self.superview.text = selectedFile
                          
                      def fileSelected(self,sender):
                          # This does not work..
                          filePreview.background_color = 'red'
                          
                  class pixelEditor(ui.View): 
                      def __init__(self, width=640, height=480):
                          self.bg_color = 'grey'
                              
                          fileButton = ui.Button(name='File', frame=(10,80,64,32), title='File Window')
                          fileButton.background_color = 'white'
                          fileButton.action = self.openFileWindow
                          self.add_subview(fileButton)
                          
                          fileLabel = ui.Label(frame=(100, 80, 300, 32), font=('HelveticaNeue-Light', 32), text='___')
                          fileLabel.name = 'File Label'
                          self.add_subview(fileLabel)
                          
                          print(self.superview)
                          
                      def openFileWindow(self,sender):
                          fv = fileWindow()
                          sender.add_subview(fv)
                          fv.present()
                          #fv = fileWindow()
                          #v.present()
                          print('File window opened.')
                  
                      
                      
                  v = pixelEditor()
                  v.present('fullscreen')
                  
                  1 Reply Last reply Reply Quote 0
                  • superrune
                    superrune last edited by superrune

                    Thanks, that also worked. I had the sub-window opening OK a couple steps back as well. But the next problem still remains, though, even in this version.

                    I do a print ('Loader superview:', self.superview) when I init the fileWindow, to see that there is a parent view I can put the selected file into, but that still returns None. Is there a way to get the new view properly assigned as a child of the first pixelEditor view?

                    edit: I see you have made a new function that prints the superview, but it returns fileWindow. So the windows parent is itself? Why is the superview not pixelEditor?

                    Thanks for taking the time to help me out!!

                    cvp 5 Replies Last reply Reply Quote 0
                    • cvp
                      cvp @superrune last edited by

                      @superrune I'll check your question but you also have another error

                              selectedFile = self['filelist'].data_source.items[self['filelist'].selected_row[1]]
                      
                      1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @superrune last edited by

                        @superrune You made a mistake: the superview is a button and my print in my new little x function is the title of the button, not the class.
                        You named your button 'File Window'....

                        1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @superrune last edited by cvp

                          @superrune Then, change in OpenFileWindow, set the FileWindow as a child of self which is the pixeleditor

                              def openFileWindow(self,sender):
                                  fv = fileWindow()
                                  self.add_subview(fv)
                          

                          And in def x():

                              def x(self):
                                  print ('Loader superview type = ', type(self.superview))
                          
                          1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @superrune last edited by

                            @superrune other big problem, when you present the FileWindow, the PixelEditor is no more presented, thus try without presenting it:

                                def openFileWindow(self,sender):
                                    fv = fileWindow()
                                    self.add_subview(fv)
                                    #fv.present()
                                    #fv = fileWindow()
                                    #v.present()
                                    print('File window opened.')
                            
                            1 Reply Last reply Reply Quote 0
                            • cvp
                              cvp @superrune last edited by

                              @superrune And last little problem, to "send" the selected file name to PixelEditor, use something like

                                      # Sends the selected image to the pixel Editor
                                      self.superview.name = selectedFile
                              

                              and you will see the name of the file on the title bar of the PixelEditor View.

                              1 Reply Last reply Reply Quote 0
                              • superrune
                                superrune last edited by

                                @cvp said:

                                print ('Loader superview type = ', type(self.superview))

                                Thanks,

                                I still have to do a fv.present() inside the openFileWindow function, right? The window will not show up otherwise...

                                print ('Loader superview type = ', type(self.superview)) now returns a NoneType, though.

                                I still want to put the selected file name into fileLabel.text - how would you go about doing that?

                                cvp 5 Replies Last reply Reply Quote 0
                                • cvp
                                  cvp @superrune last edited by

                                  @superrune some answers written before your last post, isn't it?

                                  1 Reply Last reply Reply Quote 0
                                  • cvp
                                    cvp @superrune last edited by cvp

                                    @superrune file name in label

                                            #self.superview.name = selectedFile
                                            self.superview['File Label'].text = selectedFile
                                    
                                    1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @superrune last edited by

                                      @superrune other problem

                                          def fileSelected(self,sender):
                                              # This does not work..
                                              filePreview.background_color = 'red'
                                      

                                      does not work because filePreview does not exist

                                      1 Reply Last reply Reply Quote 0
                                      • cvp
                                        cvp @superrune last edited by

                                        @superrune TableView does not have an action...I'll show you how to do, wait

                                        1 Reply Last reply Reply Quote 0
                                        • cvp
                                          cvp @superrune last edited by

                                          @superrune this

                                                  filelist.delegate = self
                                                  #filelist.action = self.fileSelected # This does not work...
                                          

                                          and this instead your fileSelected

                                              def tableview_did_select(self, tableview, section, row):
                                                  # Called when a row was selected.
                                                  self['filePreview'].background_color = 'red'
                                                  
                                              #def fileSelected(self,sender):
                                                  # This does not work..
                                                  #self['filePreview'].background_color = 'red'
                                          
                                          1 Reply Last reply Reply Quote 0
                                          • superrune
                                            superrune last edited by superrune

                                            Cool, now most of the script seems to work as it should. Thanks again!

                                            I changed the text assignment to this, and now it appears where I intended:

                                                    # Sends the selected image to the pixel Editor
                                                    self.superview['File Label'].text = selectedFile 
                                            

                                            filePreview should exist though, should it not? I created it as an imageview a couple lines up, so is should be within the scope of the file window. Looks more like the function isnt called at all when I press the different table lines.

                                            Here is how it looks right now:

                                            import ui
                                            from glob import glob
                                            from os.path import basename
                                            
                                            class fileWindow(ui.View):
                                                def __init__(self):
                                                    self.frame=(100, 150, 300, 300)
                                                    self.name = 'File window'
                                                    self.border_width = 2
                                                    print ('Loader superview at init:', self.superview)
                                                    
                                                    imagefiles = [basename(x) for x in glob('*.*')]
                                            
                                                    
                                                    filelistData = ui.ListDataSource(imagefiles)
                                                    filelistData.delete_enabled=False
                                                    
                                                    filelist = ui.TableView(frame=(10, 10 ,150, 280), data_source=filelistData, name='filelist')
                                                    filelist.row_height = 24
                                                    filelist.action = self.fileSelected # This does not work...
                                                    self.add_subview(filelist)
                                                    
                                                    filePreview = ui.ImageView(frame=(170,10,120,100))
                                                    filePreview.background_color = 'black'
                                                    self.add_subview(filePreview)
                                                    
                                                    loadButton = ui.Button(name='Load', frame=(170,120,64,32), title='Load')
                                                    loadButton.background_color = 'white'
                                                    loadButton.action = self.loadAction
                                                    self.add_subview(loadButton)
                                                    
                                                    ui.delay(self.x,0.01)
                                                    
                                                def x(self):
                                                    print ('Loader superview type = ', type(self.superview))
                                                    
                                                def loadAction(self,sender):
                                                    selectedFile = self['filelist'].data_source.items[self['filelist'].selected_row[1]]
                                                    print ('Selected ' + selectedFile + ' from sender: ' + sender.name)
                                                    # Sends the selected image to the pixel Editor
                                                    self.superview['File Label'].text = selectedFile
                                                    
                                                def fileSelected(self,sender):
                                                    # This does not work..
                                                    print('Gonk!!')
                                                    filePreview.background_color = 'red'
                                                    
                                            class pixelEditor(ui.View): 
                                                def __init__(self, width=640, height=480):
                                                    self.bg_color = 'grey'
                                                        
                                                    fileButton = ui.Button(name='File', frame=(10,80,64,32), title='File:')
                                                    fileButton.background_color = 'white'
                                                    fileButton.action = self.openFileWindow
                                                    self.add_subview(fileButton)
                                                    
                                                    fileLabel = ui.Label(frame=(100, 80, 300, 32), font=('HelveticaNeue-Light', 32), text='___')
                                                    fileLabel.name = 'File Label'
                                                    self.add_subview(fileLabel)
                                                    
                                                    print(self.superview)
                                                    
                                                def openFileWindow(self, sender):
                                                    fv = fileWindow()
                                                    self.add_subview(fv)
                                                    #fv.present()
                                                    print('File window opened.')
                                                
                                            
                                            v = pixelEditor()
                                            v.present('fullscreen')
                                            
                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors