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
-
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.
:)
The
ui.load_view(...)
function returns a normalui.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.
-
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 theother_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 inother_script
is thatui.load_view
looks for action functions and such in the script from which it is called. If we load both views frommain_script
, it will try to find all action functions there. To load the second view's action functions from the other file, we define theload_other_view
helper function that loadsother_view.pyui
from withinother_script
. That wayother_view.pyui
's action functions are always loaded fromother_script
, even ifload_other_view
is called from elsewhere.