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.


    Something wrong in my custom view

    Pythonista
    3
    4
    2020
    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.
    • Enez Houad
      Enez Houad last edited by

      I need a custom alert to wait for a task to be completed (the message changes during the task). This task can come more than one time in the app. I’ve tried many way to do this but there’s always something going wrong. As English is not my native language, it’s difficult for me to explain all my trials. Here is a simplified version of my script, can you help me please ? Thanks.

      import ui
      
      class WaitClass(ui.View):
      	
      	def __init__(self):
      		waitView = ui.View(name='waitView', frame=(0, 0, 320, 240))
      		l = ui.Label(name='label', frame=(0, 70, 320, 40))
      		waitView.add_subview(l)
      		self.add_subview(waitView)
      		
      	def present(self):
      		self['waitView'].present('sheet', hide_title_bar=True, hide_close_button=True)
      		
      	def close(self):
      		self['waitView'].close()
      		self.remove_subview(self['waitView'])
      		
      	def message(self, msg):
      		self['waitView']['label'].text = msg
      		
      	def show(self):
      		n = 100000
      		for i in range(n):
      			self.message(f'{i}/{n}')
      				
      def wait(sender):
      	v = WaitClass()
      	v.present()
      	v.show()
      	v.close()
      
      if __name__=='__main__':
      	vp = ui.View(background_color='white')
      	b = ui.Button(frame=(10, 10, 60, 30), title='Press', action = wait)
      	vp.add_subview(b)
      	vp.present('full-screen')```
      mikael cvp 2 Replies Last reply Reply Quote 0
      • mikael
        mikael @Enez Houad last edited by

        @Enez-Houad, main thing to remember with these things is that you need to let the UI thread/loop to actually display the changes you are making. For example, the tight loop you have updating the message will only ever display the last value set, and even then it loooks like you are already closing the view.

        Things to look at might be ui.delay and console.hud_alert.

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

          @Enez-Houad same kind of answer here

          1 Reply Last reply Reply Quote 0
          • Enez Houad
            Enez Houad last edited by

            Thanks for your answers, my problem is to find in English and in Pythonista what to search on the forum and that's sometimes difficult. Fortunately, there's alwais someone who is patient enough to explain ;-)
            A simple @ui.in_background was the solution…

            import ui
            
            class WaitClass(ui.View):
            	def __init__(self):
            		waitView = ui.View(name='waitView', frame=(0, 0, 240, 120))
            		
            		l = ui.Label(name='label', frame=(0, 0, 240, 40))
            		l.center = waitView.center
            		l.alignment = ui.ALIGN_CENTER
            		waitView.add_subview(l)
            		self.add_subview(waitView)
            		
            	def present(self):
            		self.subviews[0].present('sheet', hide_title_bar=True, hide_close_button=True)
            		
            	def close(self):
            		self.subviews[0].close()
            		
            	def message(self, msg):
            		self.subviews[0]['label'].text = str(msg)
            	
            	def count(self):
            		n = 100
            		for i in range(n):
            			self.message(f'{i}/{n}')
            			
            # ———————————————————————————————————————————————————————————————————
            
            @ui.in_background		
            def wait(sender):
            	v = WaitClass()
            	v.present()
            	v.count()
            	v.close()
            
            if __name__=='__main__':
            	vp = ui.View(name = 'Vue principale', frame=(0, 0, 512, 384), background_color='white')
            	b = ui.Button(frame=(10, 10, 60, 30), title='Pressez ici.', action = wait)
            	b.center = vp.center
            	vp.add_subview(b)
            	vp.present('sheet')```
            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors