set Background color with a click of a button

Well, this will be a really noobish question. But hey i am a noob :smiley:

I dont understand why the background turns to red before i click the button in the following code. Can anyone please explain.

import direct.directbase.DirectStart 
#from pandac.PandaModules import Fog
from pandac.PandaModules import TextNode
from direct.gui.DirectGui import *
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
#from direct.interval.MetaInterval import Sequence
#from direct.interval.LerpInterval import LerpFunc
#from direct.interval.FunctionInterval import Func
import sys

font = loader.loadFont("cmss12")

def genLabelText(text, i):
    return OnscreenText(text = text, 
                        pos = (-1.3, 0.95-0.05*i),
                        fg = (1,1,0,1),
                        align = TextNode.ALeft, 
                        scale = .05, 
                        font = font)
                        
def guiButton(lbl, px, py, pz, cmd):
    return DirectButton(text = (lbl),
                        pos = (px, py, pz),
                        scale = 0.1,
                        command = cmd)

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

        base.setFrameRateMeter(True)

        self.titleText  = genLabelText("TEST PROGRAM", 0)
        self.escapeText = genLabelText("ESC: Quit", 1)
        
        self.keys = {"incR" : 0, "incG": 0, "incB": 0}
        self.accept('escape', sys.exit)
        self.accept('r',        self.setKey, ["incR", 1])
        self.accept('r-up',     self.setKey, ["incR", 0])
        self.accept('g',        self.setKey, ["incG", 1])
        self.accept('g-up',     self.setKey, ["incG", 0])
        self.accept('b',        self.setKey, ["incB", 1])
        self.accept('b-up',     self.setKey, ["incB", 0])
        
        #if self.keys["incR"]:
            #r += .1
            
        #base.setBackgroundColor(r, 0, 0)
        
        guiButton("EXIT", -1.0, 0.0, 0.0, sys.exit)
        guiButton("RED", -1.0, 0.0, 0.4, base.setBackgroundColor(1, 0, 0))
        
    def setKey(self, key, val): self.keys[key] = val

w = world()
run()

Also, i am using PyPE and it gives me a warning as follows, could you also explain why Interval already exists and why it adjusts the clock:

guiButton("RED", -1.0, 0.0, 0.4, base.setBackgroundColor(1, 0, 0)) 

That code first calls base.setBackgroundColor to red and then passes the result of that function (None) to the guiButton command. You’ll probably want to do this:

(...)
def guiButton(lbl, px, py, pz, cmd, args=[]):
    return DirectButton(text = (lbl),
                        pos = (px, py, pz),
                        scale = 0.1,
                        command = cmd,
                        extraArgs = args) 
(...)
        guiButton("RED", -1.0, 0.0, 0.4, base.setBackgroundColor, [1, 0, 0])

The warnings you see on the console are harmless - they have been removed in version 1.6.0.

hey thanks for the real fast answer. I didnt know there was a 1.6 version, ill download it right away.

Meanwhile, one more noobish question.
Below code is supposed to increase the value of r and thus later on should set the background color according to.

But again it doesnt increase the value at all.

import direct.directbase.DirectStart 
#from pandac.PandaModules import Fog
from pandac.PandaModules import TextNode
from direct.gui.DirectGui import *
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
#from direct.interval.MetaInterval import Sequence
#from direct.interval.LerpInterval import LerpFunc
#from direct.interval.FunctionInterval import Func
import sys

font = loader.loadFont("cmss12")

def genLabelText(text, i):
    return OnscreenText(text = text, 
                        pos = (-1.3, 0.95-0.05*i),
                        fg = (1,1,0,1),
                        align = TextNode.ALeft, 
                        scale = .05, 
                        font = font)
                        
def guiButton(lbl, px, py, pz, cmd, args=[]): 
    return DirectButton(text = (lbl), 
                        pos = (px, py, pz), 
                        scale = 0.1, 
                        command = cmd, 
                        extraArgs = args) 

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

        base.setFrameRateMeter(True)

        self.titleText  = genLabelText("TEST PROGRAM", 0)
        self.escapeText = genLabelText("ESC: Quit", 1)
        
        self.keys = {"incR" : 0, "incG": 0, "incB": 0}
        self.accept('escape', sys.exit)
        self.accept('r',        self.setKey, ["incR", 1])
        self.accept('r-up',     self.setKey, ["incR", 0])
        self.accept('g',        self.setKey, ["incG", 1])
        self.accept('g-up',     self.setKey, ["incG", 0])
        self.accept('b',        self.setKey, ["incB", 1])
        self.accept('b-up',     self.setKey, ["incB", 0])
        
        r = 0.1
        g = 0.0
        b = 0.0
        if self.keys["incR"]:
            r += .1
        
        self.rText = genLabelText("r Value: "+str(r), 2)
        self.gText = genLabelText("g Value: "+str(g), 3)
        self.bText = genLabelText("b Value: "+str(b), 4)
            
        #base.setBackgroundColor(r, 0, 0)
        
        guiButton("EXIT", -1.0, 0.0, 0.0, sys.exit)
        guiButton("RED", -1.0, 0.0, 0.4, base.setBackgroundColor, [r, 0, 0])
        
    def setKey(self, key, val): self.keys[key] = val

w = world()
run()
        if self.keys["incR"]:
            r += .1 

You’re only checking if R is pressed once, right after you accepted the event - so that code will never execute.