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.


    Examples for dialogs.form_dialog() ?

    Pythonista
    dialogs python3
    6
    37
    18891
    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.
    • madivad
      madivad last edited by

      @cvp Do you mean itโ€™s only a problem with the beta? I am on the beta.

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

        @madivad said:

        Do you mean itโ€™s only a problem with the beta?

        I think so

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

          @cvp do you think there would be a workaround using the objc tools?

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

            @madivad I think that Objectivec is not needed and that you could use tableview_cell_for_row delegate

            ร‰dit: but you need to have your own form_dialog, I think ๐Ÿ˜ข

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

              @cvp ok, thx

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

                @madivad try this, waiting that @omz correct it

                import dialogs
                import datetime
                
                #================ copy dialogs.form_dialog: begin
                import collections
                import sys
                PY3 = sys.version_info[0] >= 3
                if PY3:
                	basestring = str
                
                def my_form_dialog(title='', fields=None, sections=None, done_button_title='Done'):
                	if not sections and not fields:
                		raise ValueError('sections or fields are required')
                	if not sections:
                		sections = [('', fields)]
                	if not isinstance(title, basestring):
                		raise TypeError('title must be a string')
                	for section in sections:
                		if not isinstance(section, collections.Sequence):
                			raise TypeError('Sections must be sequences (title, fields)')
                		if len(section) < 2:
                			raise TypeError('Sections must have 2 or 3 items (title, fields[, footer]')
                		if not isinstance(section[0], basestring):
                			raise TypeError('Section titles must be strings')
                		if not isinstance(section[1], collections.Sequence):
                			raise TypeError('Expected a sequence of field dicts')
                		for field in section[1]:
                			if not isinstance(field, dict):
                				raise TypeError('fields must be dicts')
                
                	#========== modify textfield frame: begin
                	c = dialogs._FormDialogController(title, sections, done_button_title=done_button_title)
                	for s in range(0,len(c.sections)):
                		for i in range(0,len(c.cells[s])):			# loop on rows of section s
                			cell = c.cells[s][i]									
                			print(cell.text_label.text)
                			if len(cell.content_view.subviews) > 0:
                				tf = cell.content_view.subviews[0] 		# ui.TextField of value in row
                				tf.x += 20
                				tf.width -= 20
                	#========== modify textfield frame: end
                				
                	c.container_view.present('sheet')
                	c.container_view.wait_modal()
                	# Get rid of the view to avoid a retain cycle:
                	c.container_view = None
                	if c.was_canceled:
                		return None
                	return c.values
                #================ copy dialogs.form_dialog: end
                
                form_list_of_sections = []
                
                sectionA_dicts = []
                sectionA_dicts.append(dict(type = 'text', title = 'First Name',
                key = 'first', placeholder = 'John'))
                
                sectionA_dicts.append(dict(type = 'text', title = 'Last Name',
                key = 'last', placeholder = 'Doe')) 
                
                sectionA_dicts.append(dict(type = 'number', title = 'age',
                key = 'age', placeholder='30')) 
                
                form_list_of_sections.append(('Section A', sectionA_dicts, 'Section A ends'))
                
                sectionB_dicts = []
                sectionB_dicts.append(dict(type = 'date', title = 'Date Of Birth',
                key = 'DOB', value = datetime.date.today()))
                
                sectionB_dicts.append(dict(type = 'url', title = 'Home Page',
                    key = 'homepage', placeholder = 'http://example.com')) 
                    
                form_list_of_sections.append(('Section B', sectionB_dicts, 'Section B ends'))
                
                sectionC_dicts = []
                sectionC_dicts.append(dict(type = 'email', title = 'email',
                key = 'email', placeholder = 'name@mailserver.com')) 
                
                sectionC_dicts.append(dict(type = 'switch', title = 'is_married',
                key = 'is_married', value = True))  
                
                sectionC_dicts.append(dict(type = 'check', title = 'is_registered',
                key = 'is_registered', value = False))  
                
                form_list_of_sections.append(('Section C', sectionC_dicts, 'Section C ends'))
                
                diag = my_form_dialog(title = 'Form Dialog', sections=form_list_of_sections)
                print(diag) 
                
                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @madivad last edited by

                  @madivad better with

                  			if len(cell.content_view.subviews) > 0:
                  				tf = cell.content_view.subviews[0] 		# ui.TextField of value in row
                  				if type(tf) is ui.TextField:
                  					tf.x += 20
                  					tf.width -= 20 
                  
                  1 Reply Last reply Reply Quote 0
                  • ccc
                    ccc last edited by

                    @cvp said:

                    if type(tf) is ui.TextField:

                    PEP8: Avoid directly comparing types. Use isinstance(tf, ui.TextField) instead.

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

                      @ccc ok, PEP are conventions. Why "Avoid directly comparing types"? Is there a technical reason? You have to agree that "type(tf) is xxx" is more readable than the PEP code ๐Ÿ˜€

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

                        @ccc found that, and I like it, thus, forget my previous post ๐Ÿ˜€

                        isinstance caters for inheritance (an instance of a derived class is an instance of a base class, too), while checking for equality of type does not (it demands identity of types and rejects instances of subtypes, AKA subclasses).

                        Normally, in Python, you want your code to support inheritance, of course (since inheritance is so handy, it would be bad to stop code using yours from using it!), so isinstance is less bad than checking identity of types because it seamlessly supports inheritance.

                        It's not that isinstance is good, mind youโ€”it's just less bad than checking equality of types

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

                          @cvp but but but...
                          It's a text field, why not just align it right?

                          I did just that and it's perfect now:

                          tf.alignment=ui.ALIGN_RIGHT
                          

                          :)

                          Is there a way to do that without creating our own custom dialogue type?

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

                            @madivad said:

                            Is there a way to do that without creating our own custom dialogue type?

                            I don't think so because dialogs.form_dialog does the "present" and you can't modify something after the view presentation. But perhaps somebody else could find another way.

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

                              @madivad said:

                              tf.alignment=ui.ALIGN_RIGHT

                              Good idea but sometimes, user may prefer a left alignment for text and a right alignment for numbers, thus, up to you ๐Ÿ˜€

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

                                @cvp maybe, but in any case, left aligned to the fields title is ugly to me. I can't think of a usecase where you would want it like that. Maybe in an RTL language, but I wouldn't know.

                                About the modifying after the present, yes makes sense. Having the custom dialogue type is no big issue. I'll use that.

                                Thanks for your help!

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

                                  For info, in all my big scripts I always use my own form_dialog, while keeping the standard dialogs._FormDialogController, because in my dialog views I add some buttons or I add some types like SegmentedControl or ImageView or even MkMapView, so...

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

                                    @cvp you are a machine! Great work.

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

                                      @madivad said:

                                      left aligned to the fields title is ugly to me.

                                      I can understand that.
                                      I have cases where I want left alignment but I add spaces to titles to force that all titles have same width and thus all TextFields begin at same x.

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

                                        I always use my own form_dialog,

                                        So, you can even change font, color and... of your fields, by example, in function of their name.

                                        1 Reply Last reply Reply Quote 1
                                        • cvp
                                          cvp last edited by

                                          Other solution without using an own form_dialog,
                                          but by monkey patching the class method tableview_cell_for_row of the dialogs._FormDialogController class

                                          def my_tableview_cell_for_row(self,tv, section, row):
                                          	cell = self.cells[section][row]
                                          	if len(cell.content_view.subviews) > 0:
                                          		tf = cell.content_view.subviews[0] 		# ui.TextField of value in row
                                          		if type(tf) is ui.TextField:
                                          			tf.alignment=ui.ALIGN_RIGHT
                                          	return cell
                                          	
                                          dialogs._FormDialogController.tableview_cell_for_row = my_tableview_cell_for_row
                                          
                                          diag = dialogs.form_dialog(title = 'Form Dialog', sections=form_list_of_sections) 
                                          
                                          madivad 1 Reply Last reply Reply Quote 1
                                          • madivad
                                            madivad @cvp last edited by

                                            @cvp ahh, I thought something like that would be possible. Just no idea how. Awesome, thanks.

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