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.
UI Understanding Help
-
Hi,
I am learning how to use Pythonista and am looking for advice on the best method to implement a simple idea for a script I have. I have a GUI created with the designer to set up up a crossword grid. A cell is tapped to turn it either black or white (these are buttons). However, I want another option available when tapped to produce another GUI in the form of a Tableview with a list of numbers to select (up to 26).
My problem is that I do not understand how to load the tableview I created with the designer, into view. Is it a new view requiring a frame or should it be loaded as a subview into my existing frame? All the examples I have found seem to be adding buttons to existing frames. Whereas I think I am trying to add a view which seems different. It wont load using subview with error ‘expecting’ view.
Basically I’m trying to add another GUI called NumberSelector.pyui into the process when a cell is tapped.
Is this the right way to implement my idea or should it be a different approach. If not, what am I doing wrong please?
Thanks.
## AlphaPuzzle Solver by G A ## import ui b = ui.Button() def button_tapped(sender): '@type sender: ui.Button' # Get the button's title for the following logic: t= sender.name b = sender #print(t) #b.background_color = 'black' if (str(b.background_color)) != '(1.0, 1.0, 1.0, 1.0)': b.background_color = 'white' else: b.background_color = 'black' #print(b.background_color) v = ui.load_view('AlphaGrid') if min(ui.get_screen_size()) >= 768: # iPad v.frame = (0, 0, 360, 400) v.present('sheet') else: # iPhone v.present(orientations=['portrait'])
-
At the start of your program, load both pyuis.
gridview=ui.load_pyui('AlphaGrid.pyui') numview=ui.load_pyui('NumberSelector.pyui') [.....]
then, when you want to show your numview, you can
display
it in a popover (which makes dismissing easy, but might be dodgy with a tableview)if you are using ipad, you could also simply present both as
panel
s, which allow you to simply switch tabs to alternate, no button needed.Or, for iphone, having your root view be a navigationview, then you would push_view the num selection view.
Or, you can add this as a subview of a common "root view" -- though in that case I recommend adding both your gridview and num view to a single root view, then controlling visibility using hidden and/or bring_to_front
-
@JonB Just fyi, “panel”-style presentation also works on iPhone.
-
Just the kind of pointer I needed thanks. I now have two pyui’s of which one is a tableview. I had to use global to enable the selected_row value to be used outside the function, is this the norm? Anyway thanks very much for your help.
Regards
Gary -
Globals are a common shortcut when using UI. A cleaner approach is to use a custom view class, as this lets you encapsulate all of your logic in one class, and you can use instance attributes in place of globals. Custom views are a little tricky to get working in the pyui designer though.