How to hide the menu?

Hello i have an problem to hide my menu.
Here is the code:

When is set self.menu1 = 1 to = 0 i become this error:
AttributeError: ‘function’ object has no attribute ‘hide’

Can anyone please help me?

		self.menu1 = 1
		self.menu()
	
	def menu(self):
		if self.menu1 == 1:
			self.startb = DirectButton(pos = Vec3(0,0,.6), text = "Start",
							scale = .1, pad = (.5, .5), text_font = font,
							rolloverSound = None, clickSound = None,
							command = self.startbutton)

			self.exitb = DirectButton(pos = Vec3(.0,0,.0), text = "Exit",
							scale = .1, pad = (.5, .5), text_font = font,
							rolloverSound = None, clickSound = None,
							command = self.exitbutton)
			
		else:
			self.menu.hide()

	def startbutton(self):
		import testworld
		w

	def exitbutton(self):
		self.exitb.hide()
		sys.exit()

Please use code tags instead of quote ones. It makes things easier to read.

Issues:
startbutton is bad. What are you doing with an import inside a method? All imports should be at the top. Also, style wise, you its nice if you capitalize the first letter of works in method names ex: startButton

Anyway, a real issue is that w by itself is not a very good line of code. How did that get in there?

It appears that you are using self.menu as a method (self.menu()) and as an object (self.menu.hide()). Methods won’t have a .hide(), and you can’t call an object. self.menu.hide() is trying to call .hide() on your menu method.

Which is it? If you intent for it to actually be both, I’m not sure if python allows you to have a method and object in the same scope with the same name

I will place the import on the top of the code.
the w is for an class in an other file w = world
The startbutton works.

I want to hide the complete menu that is definied in de menu.
Because when the world is loaded in “startbutton” the menu don’t close.

is this what you want?

def hide(self):
   self.startb.hide()
   self.exitb.hide()

def menu(self):
   if self.menu1 == 1 :
      ...
   else:
      self.hide()

Yes that it’s but it don’t work right because when i set self.menu1 == 0
i become this: AttributeError: gui instance has no attribute ‘startb’

class gui(DirectObject):
	def __init__(self):

		self.title = OnscreenText(text="Test-GUI", font=font,
									style=1, fg=(1,1,1,1), pos=(0,0.95), scale = .07)

		self.accept('escape', sys.exit)

		base.disableMouse()

		self.menu1 = 1
		self.menu()

	def menu(self):
		if self.menu1 == 1:
			self.startb = DirectButton(pos = Vec3(0,0,.6), text =
"Start",
							scale = .1, pad = (.5, .5), text_font = font,
							rolloverSound = None, clickSound = None,
							command = self.startbutton)

			self.exitb = DirectButton(pos = Vec3(.0,0,.0), text =
"Exit",
							scale = .1, pad = (.5, .5), text_font = font,
							rolloverSound = None, clickSound = None,
							command = self.exitbutton)

		else:
			self.hide()

	def hide(self):
		self.startb.hide()
		self.exitb.hide() 

	def startbutton(self):
		import testworld
		w

	def exitbutton(self):
		self.exitb.hide()
		sys.exit()

w = gui()
run()

import testworld in in def startbutton because when i set in on the top it loads the world from the other file and i don’t want this. This world should be loaded in startbutton when it is clicked.

The class from the other file is class World(DirectObject):

I think that’s because self.startb was not declared, it is created when you call self.menu() only if self.menu1 is 1, so are you sure you need this self.menu1 variable?

try this:

class gui(DirectObject): 
   def __init__(self): 

      self.title = OnscreenText(text="Test-GUI", font=font, 
                           style=1, fg=(1,1,1,1), pos=(0,0.95), scale = .07) 

      self.accept('escape', sys.exit) 

      base.disableMouse() 

     self.startb = DirectButton(pos = Vec3(0,0,.6), text =
"Start", 
                     scale = .1, pad = (.5, .5), text_font = font, 
                     rolloverSound = None, clickSound = None, 
                     command = self.startbutton)
     self.startb.hide()       

     self.exitb = DirectButton(pos = Vec3(.0,0,.0), text =
"Exit", 
                     scale = .1, pad = (.5, .5), text_font = font, 
                     rolloverSound = None, clickSound = None, 
                     command = self.exitbutton) 
     self.exitb.hide()

      self.menu1 = 1 
      self.menu() 

   def menu(self): 
      if self.menu1 == 1: 
         self.startb.show()
         self.exitb.show()
      else: 
         self.hide() 

   def hide(self): 
      self.startb.hide() 
      self.exitb.hide() 

   def startbutton(self): 
      import testworld 
      w 

   def exitbutton(self): 
      self.exitb.hide() 
      sys.exit() 

w = gui() 
run() 

Now it works i have removed self.menu1 because not really need. But when the world is loaded the gui don’t close.
Must i edit the world.py for that?

in your startButton function call self.hide() so that your function will hide your gui elements.

Or it don’t work because this here:

   def startbutton(self):
      import testworld
      w

I’m importing the world there?

Thx now it works :slight_smile: