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.
JonB last edited by
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.
RocketBlaster05 last edited by
@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?
JonB last edited by JonB
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()
mikael last edited by
@RocketBlaster05, you do not use
globalto declare something as global.
push2is global because it has been defined in the global scope.
You only use
globalwithin the function to declare that you want to change the value of the global variable.
cvp last edited by
@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!