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 button help
-
Hello, im brand new to the forum and im looking for some help on the UI module that I couldn't find in the documentation. What I'm trying to do is build a very simple neural network that looks at a three by three grid of buttons that can be on or off then inputed to the net by pressing a "Enter Inputs" button. However this requires a "action" attached to the "Enter Inputs" that can find the current titles of all buttons on the grid (either 1 or 0), but I dont know how to access button attributes not of the "sender" especially since I can't load my .pyui file and its contents before defining the relevant functions. I've tried every iteration of the code possible moving things around even trying to access non existing subclasses but all i've got so far is this:
import ui import neural_network def button tapped(sender): # used by all my buttons but "Enter Inputs" if sender.title == '0': sender.title = '1' elif sender.title == '1': sender.title = '0' def Enter_inputs_tapped(sender): pass # dont know what too put that could access all button titles. v = ui.load_view('NetworkUI.pyui') v.present('sheet')
I know the answer is probally stupid simple, but I could use help. Just so you know the UI also contains a label for my eventualy output after linking this to the neural network, but is not mentioned in the current code as that appears easy and I won't worry or code that until much later. I didnt include the UI json as its fairly big and I didnt want it taking up my post when I dont think its nesessary to include. Any help or suggestion would be awesome.
Note: In case my wording or grammer was weird all I want to do is have a action attached to the button that takes the current values of the other nines buttons titles and send it to my neural network. In case you couldnt tell Im definitely an amateur.
-
Use sender.superview to get access to your main view, in whch case you can then reference other views by name
sender.superview['button1']
or through the subviews attributesender.superview.subviews[0]
.Another approach sometimes used is after you load your view, give it a global name. Even though your action is define before the global, as long as the global name exists before the action is called, it will still see it.
def action(sender): #if v is not found in locals at runtime, #globals is searched next, so this works even though v is not yet defined print(v) v=load_view() v.present()
-
@AI-subroutine , I think I understand your question. There are quite a few ways to achieve this. But maybe a very useful way for you might be to Use the following way below. As once you get this idea, you will not feel as detached from the main view as you probably feel now.
Except for your topmost view, all other views/btns with have a superview. The view they reside in. All views have a subviews attr. A list containing all the views that have been added to that subview.Remember, all the ui controls (except ui.ButtonItem ) are views or the base class is a view.
The example below is a quickie, I think I did type wrong. But you could be checking any attribute of the object you like to determine if it was your object of intrest. Like the name attr for example.
Ok, hope it helps
def button_action(sender): superview = sender.superview for obj in superview.subviews: if type(obj) == ui.Button: pass
-
def button_tapped(sender): sender.title = str(int(not int(sender.title))) # toggles between 0 and 1
But since these buttons are toggles, perhaps they should be ui.Switches instead of UI.Buttons.
-
How can I access a subview (i.e. a label), that is in another subview of a NavigationView?
Expample: I have a Navigation View. Inside live two Subviews. Subview 1 has a label in it, subview 2 has a button. How can I change the label of the text, when the button is pressed? (Navigation View and the two SubViews are three different files. I have described my setup in another thread.)
-
@vcr80 Could you post some sample code on github for us to look at?
-
@vcr80 , I just did a iteration above to show you can get to the superview and then subviews without knowing what's there.
But if I think you are asking for is Something likelb = sender.superview.subviews['my_nav_view'].subviews['my_label']
Lb.text = 'hello'Should not matter they are loaded from different files. They are just views in the hierarchy.
Maybe I misunderstood you.
-
sure, @ccc here it is: https://gist.github.com/nitricware/f82416c411586404e5d2
unfortunately the iteration you posted, @Phuket2 just lists the subviews of the view where the pressed button is located... but not the subviews of the parent navigation view nor the subviews of the root view, that itself is loaded into the navigation view...
-
One of many solutions is to load your sub view just once as a global variable...
subview = ui.load_view('SubView.pyui') def pressButton(sender): dialogs.hud_alert('okay2') subview.name = 'What happened?!?' class NavView(object):
-
@vcr80 , but really just stop and think for a moment. No funky solution really needed. If you can understand the superviews and subviews you can find everything. I can't look at your Github code at the moment
-
Great, @ccc - that cleared things up!
I was able to set the text of a label inside another subview of the navigation view now.However, I still don't see how I could access the navigation view itself when a button inside the the subview is pressed.
NavView.push_view(root)
andNavView.v.push_view(root)
didn't work. Knowing how to access the navigation view from within a subview could come in handy if I'd like to close the current subview as soon as a button is pressed or if i want to load yet another subview when the button is pressed.Updated Gist: https://gist.github.com/nitricware/468b31a1b5363c4474e1
Lines 10 and 42 show my new issues...@Phuket2 I'd like to! Did I miss some kind of introduction to Pythonista UI creation? Or are you supposed to know how those kind of things work (Installed Pythonista less than a week ago.)?
-
Actually @Phuket2 it is not so intuitive when NavigationViews are involved... root_view, sub_view, and nav_view are three separate views and none of them are the subviews or the superviews of each other.
It is possible for sender, root_view, and sub_view to get to nav_view via their .navigation_view instance variable. However there is no convenient way for sender to easily get to subview except via a global or a class instance variable.
-
@vcr80 NavView can just be an object instead of a ui.NavigationView. See comments on the bottom of the gist.
-
@vcr80 , no I was not trying to be smart. But @ccc mentions that in the navigation view things are more disjointed. I have never used it. I am not sure how navigation view stands now, but for a long time it had known problems.
But still regardless of the disconnects with the views in navigation view, if you know how to get around the views in Pythonista , a myriad of solutions will come to you. You really have to understand them. I really sheds a lot of light on many things.
But really just trying to help. -
@ccc , fair enough. But given that you know it, you could dynamically create attrs for the object that has a callback func defined to point to something useful. Or a custom ui.View wrapper class. Well there are many ways.
I am not good. But with knowledge of views, I am never stuck to derive a reference to any ui component -
@ccc , I just did some work with ui.Navigation. But in all honesty this is not ready for prime time yet. Can write it yourself with out the issues.
There are 2 methods and 3 attributes and a very explicit required startup view.
I can see @omz idea, but it has become an orphan. My idea would be he should remove it until he has more time to devote to it. Personally, I would say it's not useful enough in its current state to publish. Actually the reverse, new users will see the promised land, but it's not there yet. Will not get them to the end of a solution and they will struggle with it.
Look, I am still a newbie, but that's my view 😱😎
If I am wrong then so be it -
Thanks for all the replies! Superview works great!
-
@ccc said:
Actually @Phuket2 it is not so intuitive when NavigationViews are involved... root_view, sub_view, and nav_view are three separate views and none of them are the subviews or the superviews of each other.
It is possible for sender, root_view, and sub_view to get to nav_view via their .navigation_view instance variable. However there is no convenient way for sender to easily get to subview except via a global or a class instance variable.
On this one I think there is a solution using dynamic attrs.
But I still get your point. But as I said in another post, I don't think this class/component offers enough functionality yet to even use it.
A view, with a list or 2 popping some items and some styling attrs. -
Thanks, @ccc - That works fine! Could you explain the part with
NavView(object)
? Why did you change it and what does it change in the background? The rest of your changes are clear to me.Sorry, @Phuket2 I didn't mean it in any offensive way. I am really searching for an introduction to UI Designer of Pythonista because I really want to know how things work. I too think that understanding how views work will help a lot! So again: did I miss something or are you simple supposed to find out on your own or should one already know how those things work because one should know how Python works?
-
@vcr80 , hey no problems. Also I was not either. Sometimes words can seem a little hash when they are not meant to be. I think as I have become more relaxed here, I write a little differently, as I sort of assume people start to get to know your meaning with not so many words.
Anyway. Regarding leaving about the ui the built in docs are really good. Some things missing. But still good. Also is the Pythonista Tools site/repo. Here is a link into the ui part of the Pythonista Tools
http://pythonista-tools.github.io/Pythonista-Tools/scripts.htmlAlso a lot of the guys public repo's on git hub are very good place to find good examples. @ccc , @JonB , @TutorialDoctor and not forgetting @omz. That's just few that come to mind
Another great place is searching this forum. The searching is a little hit and miss for me here. But, also great advise given by these guys.
Look I am still a newbie. At the same time I still have to challenge ideas. @ccc and many others here have taught me so much and still do. But occasionally I still have to challenge thier ideas 😱
Normally I crawl,away with my tail between my legs.