A simple button.

As one of the world’s worst programmers I have this habit of getting stuck on the simplest things.

For several days I’ve been trying to make a button that works.

My code:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.gui.OnscreenText import OnscreenText
from direct.gui.DirectGui import *
from direct.task.Task import Task
from pandac.PandaModules import TextNode

class App(DirectObject):
    def __init__(self):
        self.gameTask = taskMgr.add(self.loop, 'loop')
        
    def buttonPressed(self):
        print "The button was pressed." 
        
    def loop(self, task):
        b = DirectButton(text = "Button", pos=(0,0,0), scale=0.2, command=self.buttonPressed)
        return task.cont
    
  
the_app = App()
run()

What I am trying to do is have each part of the game controlled by a task. In this case I am trying to make a main menu. The idea is during initialization create a task that creates the main menu (in this case, for now, one button), and when a button is pressed, end the task (and do whatever cleanup/removal is necessary) and start another task that does something else like run the game or display some other menu.

Why does my button not work/why does it not work how I think it should? Should I be doing this differently?

Afaik a task is called every frame so you generate n-buttons in n-frames. But you only need one button (its is drawn automatically every frame by the engine until it is destroyed). So create the button just once (i.e. during the init) and only destroy /create new if you want to change the GUI.

Thank you. I had a feeling it was something simple.