How to open new ui views through buttons on respective view?
Just started with Pythonista and trying to write a script where I can open new ui views (full screen) through buttons on respective view.
Have tried different ways, trying to load all views at once and then switch using view.send_to_back () / view.bring_to_front (), but dont get it to work. Have also tried to put each view in functions and calling them, then close the old one with view.close (), but neither get this to work.
Can anyone please explain the best way to solve this?
@guran, if all your views are subviews of the same root view, then `bring_to_front‘ should work.
Alternative is to use
hidden, but that needs more bookkeeping.
Probably I miss the most fundamental when it comes to managing subviews. Have tried with both bring_to_front / back + hidden but they don't seem to see each other.
Below is my non-working script... world be grateful to get some concrete tips on how to handle this, if possible?
import ui #import ui, itertools class SwitchViews(ui.View): def __init__(self): self.add_subview(ui.View()) #self.background_color = "blue" #self.present('fullscreen') def view1(): view_start_lst1=SwitchViews() view_start_lst1.present() view_start_lst1.background_color = ('red') def b_tap1(sender): view_start_lst1.hidden = True view_start_lst2.hidden = False #view_start_lst1.send_to_back() #view_start_lst2.send_to_front() b1 = ui.Button() b1.title = ' Switch to next view' b1.frame = (100,100,300,100) view_start_lst1.add_subview(b1) b1.action = b_tap1 def view2(): view_start_lst2=SwitchViews() view_start_lst2.present() view_start_lst2.background_color = ('green') def b_tap2(sender): view_start_lst2.hidden = True view_start_lst1.hidden = False #view_start_lst2.send_to_back() #view_start_lst2.send_to_front() b2 = ui.Button() b2.title = ' Switch to next view' b2.frame = (400,100,300,100) view_start_lst2.add_subview(b2) b2.action = b_tap2 SwitchViews() view1() view2() ```
@guran, there’s a bit too much present and SwitchViews there for it to work. See below one way to get alternating views.
import ui class SwitchViews(ui.View): def __init__(self, **kwargs): super().__init__(**kwargs) self.flex = 'WH' b1 = ui.Button( title=' Switch to next view ', frame=(100,100,300,100), action=self.tap, background_color='white', tint_color='black' ) self.add_subview(b1) def tap(self, sender): self.other_view.bring_to_front() v = ui.View() view1 = SwitchViews( background_color='blue', frame=v.bounds) view2 = SwitchViews( background_color='green', frame=v.bounds) v.add_subview(view1) view1.other_view = view2 v.add_subview(view2) view2.other_view = view1 v.present()
Big thanks for the help!
I can see that it works even though I don't really understand why. As I understand it the objects view1 and 2 are created from the SwitchView class, which in turn is a subclass of the ui.view class. These are added to the object v created directly from the ui.view class. Then view1 and 2 are then added to the object v as subviews.
If I understand it correctly, why does it work and do you know where I can learn more to understand the logic?
@guran, first, make sure that you have read the help on the ui module.
In Pythonista, you do not really have windows, only views.
Usually, you only
presentone view, the root of your view hierarchy, which will fill the screen. Then you add other views as subviews so that they form a hierarchy, subviews also visible ”above” their parent views. Generally, a subview will not be visible outside the bounds of its superview, but it can of course be smaller.
Think about views as rectangles, layered by their subview relationships, and you should be ok.