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.
How to Access global Booleans with if statements?
-
So I have been messing around with Navigation Menus and I have a script that will open up various pages back to back to back. I wished to regulate this with buttons, and that the buttons would allow the various pages to pop up. I had it set so that the buttons would set a global variable to True or False, and the script would recognize that once the variable was True, it would open up a new page on the nav menu. However the script does not recognize any of the global variables. Here is the code below. Any Ideas for the variables or am I trying to do this incorrectly?
import ui from scene import * global push2 push2 = False global push3 push3 = False global push4 push4 = False global push5 push5 = False def waterbottle_pressed(Water_Bottle): #custom UI Buttons global push2 push2 = True def cardboardbox_pressed(Cardboard_Box): global push3 push3 = True def glass_pressed(Glass): global push4 push4 = True def paper_pressed(Paper): global push5 push5 = True class RecycleMenu(): def __init__(self): self.background_color = 'white' self.start_NavView() def start_NavView(self): self.view = ui.load_view('RecycleMenuOptions') #custom UI View self.view.background_color='white' self.view.name='View1' nav = ui.NavigationView(self.view) nav.present(hide_title_bar=True) if push2==True: #the following are filler tests view2 = ui.View() view2.name='View2' view2.background_color='blue' nav.push_view(view2) if push3==True: view3 = ui.View() view3.name='View3' view3.background_color='#e5ffaa' nav.push_view(view3) if push4==True: view4 = ui.View() view4.name='View4' view4.background_color='#717171' nav.push_view(view4) RecycleMenu() #Code execute
-
As a side note when the variables are set to True by default they work so I was wondering how to get them to change.
-
Your start Navview function is called only once -- before your button actions have been called. So of course, the booleans are false.
If you want your button actions to do something, they will need to call out to the function that does something.
-
@JonB I’m sorry but my mind is blanking right now how should I call on the function in order for it to reset again?
-
For starters you might break all of the code starting with the if statements into a new method. Then, store your object in a variable.
# after start method: def update_menu(self): #if.... m=RecycleMenu() Then in your actions, call m.update_menus()
-
@RocketBlaster05, you do not use
global
to declare something as global.push2
is global because it has been defined in the global scope.You only use
global
within the function to declare that you want to change the value of the global variable. -
@RocketBlaster05 with all of previous advices, perhaps this (not tested) could be sufficient
import ui from scene import * def waterbottle_pressed(Water_Bottle): #custom UI Buttons m.update_menus(nam='View2', col='white') def cardboardbox_pressed(Cardboard_Box): m.update_menus(nam='View3', col='#e5ffaa') def glass_pressed(Glass): m.update_menus(nam='View4', col='717171') def paper_pressed(Paper): m.update_menus(nam='View5', col='red') class RecycleMenu(): def __init__(self): self.background_color = 'white' self.start_NavView() def start_NavView(self): self.view = ui.load_view('RecycleMenuOptions') #custom UI View self.view.background_color='white' self.view.name='View1' nav = ui.NavigationView(self.view) nav.present(hide_title_bar=True) def update_menus(self, nam=None, col=None): v = ui.View() v.name = nam v.background_color = col nav.push_view(v) m = RecycleMenu() #Code execute
-
Oh My Gosh thank you all so much! @JonB @mikael @cvp All of your suggestions worked very well, especially yours cvp. Everything worked perfectly, and the only thing I had to add was to declare the "nav" variable as a global. Thank you all so much for your help with this. My app is finally coming together!