Moving from one *.pyui file to another
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?
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.
ui.load_view(...)function returns a normal
ui.Viewobject, 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.
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
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
ui.load_viewlooks 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_viewhelper function that loads
other_script. That way
other_view.pyui's action functions are always loaded from
other_script, even if
load_other_viewis called from elsewhere.