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.


    Moving from one *.pyui file to another

    Pythonista
    2
    4
    4604
    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.
    • mrcoxall
      mrcoxall last edited by

      Is there a way to be in one *.py file with its corresponding *.pyui file that creates the GUI.
      Then you click a button and go to another *.py with its corresponding *.pyui file?

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

        I assume you're talking about presenting two UIs from different pyui files. If you want to switch between two pyuis while editing, open them in two tabs. :)

        The ui.load_view(...) function returns a normal ui.View object, so you can do anything you want with it. There is no need to present the view directly. For example, you can load both views at the start of your script, but only present the first one at the start. For example:

        import ui
        
        main_view = ui.load_view("main.pyui")
        other_view = ui.load_view("other.pyui")
        
        # Action of the button in main_view that should show the second view
        def my_button_action(sender):
            other_view.present("sheet")
        
        main_view.present()
        

        This is a very simple example, it presents the second view in sheet mode on top of the first one, and you can close the second one to get back to the first one. If you don't want the user to go back, you can close the main view before presenting the second one.

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

          That works and I get the new view.
          Problem is it looks like Pythonista is trying to load the buttons that are defined in the second *.pyui file, but the first *.py file does not have any reference to them. The are in the second *.py file.

          Warning: Could not bind action: name 'calculate_button_touch_up_inside' is not defined
          Warning: Could not bind action: name 'clear_button_touch_up_inside' is not defined

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

            Ah, if you want to separate the Python files too, then you need to get one file to load the other. This should be easy to do by putting both scripts into the same folder (which you probably did already) and then importing the second file from the first:

            # In main_script.py:
            import other_script
            

            Now, to present the second view from the main script, but using the functions from other_script, we have to use a small trick. In the other_script, create a function that looks like this:

            # In other_script.py:
            def load_other_view():
                return ui.load_view("other.pyui")
            

            Then you can call this function in your main script to load the other view:

            # In main_script.py:
            import ui
            import other_script
            
            main_view = ui.load_view("main.pyui")
            other_view = other_script.load_other_view()
            

            The reason why we need to define the extra load_other_view function in other_script is that ui.load_view looks for action functions and such in the script from which it is called. If we load both views from main_script, it will try to find all action functions there. To load the second view's action functions from the other file, we define the load_other_view helper function that loads other_view.pyui from within other_script. That way other_view.pyui's action functions are always loaded from other_script, even if load_other_view is called from elsewhere.

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