How to change font color of DirectButton on mouse over?

My Code Snippet. This takes the text from the array links and creates a DirectButton object for each “Link” in the array. These buttons appear on the right side of the screen in a vertical arrangement.


links = ["LINK 1","LINK 2","LINK 3","LINK 4"] #The text to be placed into the buttons

z = 0
for i in range(len(links)):
    linkButton = DirectButton(text = (links[i]), scale=.09, relief=None, text_fg = (0.30,0.35,0.35,1), text_font = loader.loadFont('fonts/Impact.ttf'))
    linkButton.setPos(1.35, 0, z)
    linkButton.bind(DGG.WITHIN, linkButton.configure(text_fg = (1,1,1,1)))
    z = z - 0.15 

This piece of code i am working on is intended to display buttons on a menu page. I want to be able to change the font color for each link when the mouse hovers over the link, but it seems im not understanding how the bind class works. This example prints an Assertion error. Can anyone give me any suggestions on how i can get the mouseOver event to change the font color?

EDIT: I am using version 1.9.0 on Ubuntu Utopic

Hmm… It’s a pity that the text-colour doesn’t seem to be one of the parameters that DirectButton allows one to specify as a list of values, one for each state…

That said, you could perhaps create four DirectLabels, one of each colour, and, storing them in a list, pass them into the DirectButton’s “geom” parameter.

Regarding “bind”, I think that the problem is that “bind” expects a function-reference, and an optional set of extra arguments; what you’re currently passing to it, I believe, is the result of calling “linkButton.configure”, which may well be “None”. In short, if your code includes a method-name followed by the relevant brackets and parameters, that method is (in general) called there and then; the method-name alone provides a reference to that method.

Consider this quick example:

def mew(loudness):
   return "you mew " + loudness

val = mew("loudly")
# val now holds the string "you mew loudly"

val = mew
# val now holds a reference to the function "mew"

In terms of “bind”, I think that something like this might work:

#  I'm assuming that this takes place within an object,
# hence the use of "self"; if not, adjust accordingly.
    linkButton.bind(DGG.WITHIN, self.setButtonColour, extraArgs = [linkButton, (1, 1, 1, 1)])

# Elsewhere:
def setButton(self, btn, colour):
    btn["text_fg"] = colour

Your solution worked, so I thank you for that one it was Much Appreciated. This even helped to further explain the usage of extraArgs for me, and how it ties in with object execution. However, I kept getting a typeError saying that it takes 2 arguments and 3 were given, so i added an extra parameter to setButtonColour to get around this.