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.
NavigationView Questions
-
-
class My_Very_Special_World_View(ui.View): # add the definition of the get_entered_values() function here def will_close(self): # This will be called when a presented view is about to be dismissed. # You might want to save data here. print('will_close() called with {} subviews'.format(len(self.subviews))) print(self.get_entered_values())
-
Excellent. Thank you @ccc, I will have to play with that code.
-
Hi All,
I'm back with another question about Navigation Views.
In the ui builder I have a main view with a Label and a Navigation View. Then, inside the navigation view I have a variety of buttons and tables.
Now, when I go to the script side and query the navigation view for a button, I get nothing
v = ui.load_view() nv = v['nav-view'] print nv['button1'] ## 'None'
How do I get the sub views of my navigation view that I set up in the ui editor? Or do I have to add the sub views manually?
Thank you,
B. -
Edit: Not quite right for NavigationViews...
You can always build yourself a roadmap by walking the subview tree:
for i, sv in enumerate(v.subviews): print(i, sv.name) for i, sv in enumerate(v['nav-view'].subviews): print(i, sv.name) # Once you know where things are you can: button1 = v['nav-view']['button1'] # subview of a subview
-
@ccc I had tried doing that, but in hopes that yours would work I ran it and got this:
(0, u'label1') (1, u'nav-view')
It seems like if you add subviews to a navigation view in the ui editor, it doesn't go through to the script editor side. I checked the
pyui
file in a text editor and saw that the button was in fact anode
of the navigation view in thepyui
file. I may just create a seperate UI file and import it in. At least that will get going in the right direction... -
NavigationView
s are a bit special... Adding views to aNavigationView
in the UI editor won't actually add them as subviews, but rather create an empty view, add the subviews to that, and then basically call theNavigationView
's constructor with that view, so it becomes the root of the navigation tree... The wholeNavigationView
class is a bit under-developed to be honest, and there's not really a good way to access views that you add in the UI editor directly. If you need that, you might want to construct the navigation view programmatically for now. -
Navigation views have a back button in the upper left corner that appears after a second subview is pushed. Can anyone tell me how to detect a user tapping that "back" button within a program?
-
I created three files:
A pyui file and a corresponding py file (MyView.pyui and MyView.py) plus a subview pyui file (MySubView.pyui)I then was able to create a button in the main view (where the navigation view is located) which, when pressed, pushes the subview into to navigation view. However doing it that way, makes this button stay, no matter which root/subview is visible inside the navigation view.
How can I create a right_button_item inside MyView.py= I tried
v = ui.load_view v['navigationview1'].right_button_items = ui.ButtonItem[...]
But that does nothing at all
v = ui.load_view v.right_button_items = ui.ButtonItem[...]
Adds it to the top level view.
What do I miss?
-
@vcr80 If possible, please copy and paste code directly from your script and don't type it out from memory. The code snippets that you posted would cause a few
TypeError
s when you'd try to run them (disregarding the...
s - their meaning is clear enough IMHO). If the code you post here is different from what's in your script, then it can be difficult to help, because there's no way to tell what is a typo and what is a mistake in your real code.Anyway, the issue here seems to be that you're setting
right_button_items
on theNavigationView
. If you want to put the buttons in the same toolbar where the "Back" button is, then you need to put them in theright_button_items
of the view that's in theNavigationView
.However last I checked this is not possible if you create the
NavigationView
through the UI builder. The issue is that the initial view in theNavigationView
is created "anonymously" byload_view
, meaning that there is no way to access itsright_button_items
. This means that you'd need to create theNavigationView
in Python from an existing view that is accessible in some way so you can give itright_button_items
. -
@dgelessus you're right. sorry! Next time, I'll copy the code!
I changed my code based on your answer: I created a third pyui file for the root view and pushed the root view via code to the navigation view rather than in the UI Builder and I set the button_right_items in the code that loads the root view.
That worked!
Here's exactly what I did in case someone is stuck with the same very basic problem:
I created a script file with a UI file. They share the same filename (say NavView.py and NavView.pyui). In The pyui file, all I did was set the Custom Class View to
ui.NavView
. In its corresponding py-file, i createdclass NavView(ui.View)
. Also, I created two stand alone UI files for designing the root view and the sub view.This is the class:
# coding: utf-8 import ui class NavView(ui.View): def __init__(self): # Load the default view that's placed inside the Navigation View. # This root view has its own pyui file (here: RootView.pyui). # That makes it easier to design the root view. root = ui.load_view('RootView.pyui') # Set the titlebar text of the Navigation View when the root view is loaded root.name = 'Root View' # Specify the visible buttons in the navigation view titlebar when the root view is visible. root.right_button_items = [ui.ButtonItem(action=self.openSubView, image=ui.Image.named('ionicons-close-24'))] # Create the Navigation View with the root view preloaded self.v = ui.NavigationView(root) self.v.present('sheet') def openSubView(self,sender): # Load the sub view thats loaded when the defined button in the Navigation View is pressed while the root view is loaded sub = ui.load_view('SubView.pyui') # This text will be displayed as the Navigation View's titlebar text sub.name = 'Sub View' # Display the sub view instead of the root view self.v.push_view(sub) # Call the class NavView()
If you have any suggestion on how to improve the code, I'd love to hear it!
The above code works great as long as I don't actually want to do anything with the code. I'm not getting it. Did I miss any kind of tutorial or documentation?
Say I have a button in one of the sub views. How do I make the button (with action set to
buttonFunction
) do anything?def buttonFunction(sender)
below the class doesn't work.def buttonFunction(self, sender)
inside the class doesn't work either. And it doesn't matter wether every view (not just the Navigation View Container pyui) or just the top level pyui that has the Navigation View in it has its custom view class set to ui.NavView or not.I'm really confused since I can't find anything like a introduction to Pythonista UI Designer...