In working on a custom DirectGUI sub-class, I discovered something: it seems that DirectGUI discards component-related constructor-keywords–that is, keywords that contain an underscore–after initialisation. They’re used for any construction done during that period, but appear to be lost afterwards.
As a result, components constructed after initialisation aren’t provided any such arguments, and have only the default styling.
I bumped into this previously, I think, when dealing with DirectOptionMenu–I found that if its item-list was changed after construction, the resultant popup-menu lost any stylisation applied via constructor-keywords.
However, I hesitate to post this to the GitHub issue-tracker, for two reasons: First, I may well be missing something here; I don’t fully understand what DirectGUI does behind the scenes, I fear. And second, it I am correct, it seems possible that this issue is sufficiently fundamental that it wouldn’t be feasible to fix it.
Thus I want to first open the matter for discussion here!
A quick example-program that shows the problem (on my machine, at least):
from direct.task import Task
from direct.showbase import ShowBase as showBase
from direct.gui.DirectGui import DirectFrame, DirectButton
class Mew(DirectFrame):
def __init__(self, parent = None, **kwargs):
optiondefs = (
('frameSize', (-0.5, 0.5, -0.5, 0.5), None),
)
self.defineoptions(kwargs, optiondefs)
DirectFrame.__init__(self, parent)
self.buttonCounter = 0
self.addButton()
self.initialiseoptions(Mew)
def addButton(self):
btn = self.createcomponent("Mew%d" % self.buttonCounter, (), "mewButton",
DirectButton,
text = "Mew",
pos = (0, 0, 0.4 - self.buttonCounter*0.1),
scale = 0.1)
self.buttonCounter += 1
class Game(showBase.ShowBase):
def __init__(self):
showBase.ShowBase.__init__(self)
self.frame = Mew(mewButton_frameColor = (0, 1, 0, 1))
self.accept("escape", self.userExit)
self.accept("space", self.frame.addButton)
app = Game()
app.run()
Note that the first call to “addButton”–done in the constructor, before “initialiseoptions”–produces a button with the specified green “frameColor”, but that later calls produce buttons of the default grey.