Make a DirectButton run multiple commands

I’m having an issue where I need to make a DirectButton run multiple commands to properly transition to a later screen. Basically, I want it to load the next page (Which I can do) and I want it to stop the current background music so it can start some new background music (Which I can do) but I can’t get the button to do both. This is really annoying me so I though it’d be a little easier if I asked around. Here’s a quick snippet of code.

def StartToon():
	ToonSelect.destroy()
	ToonText.destroy()
	ExitText.destroy()
	ToonSelect1.destroy()
	ToonSelect2.destroy()
	ToonSelect3.destroy()
	ToonSelect4.destroy()
	ToonSelect5.destroy()
	ToonSelect6.destroy()
	ExitButton.destroy()
	backgroundMusic = loader.loadSfx("phase_3/audio/bgm/create_a_toon.ogg")
	backgroundMusic.setLoop(True)
	backgroundMusic.setVolume(0.25)
	backgroundMusic.play()

backgroundMusic = loader.loadSfx("phase_3/audio/bgm/tt_theme.ogg")
backgroundMusic.start()

ToonSelect1 = DirectButton(text_font = ToonFont, text_fg = (0.433594, 0.90625, 0.835938, 0.7), text_pos = (0, 0.27), text_scale = (0.10, 0.10, 0.10), text_wordwrap = 5, text_align = TextNode.ACenter, text = "Make A Toon", relief = None, command=(StartToon, backgroundMusic.stop))

There’s more to it already, but I really don’t need to reveal more. Also, bonus points or whatever to whoever realises what I’m trying to make. Also, where it does say the button’s command, that is only my latest attempt, not all I tried.

The simplest solution might be to make a method that includes the various lines that you want to have the button run, and then assign that to the button’s command argument; something like this:


def selectToonPressed():
    StartToon()
    backgroundMusic.stop()

def StartToon():
    <lines omitted for brevity and clarity>

    ToonSelect1 = DirectButton( <various arguments omitted for brevity and clarity>,
                               command = selectToonPressed)

(Make sure that “backgroundMusic” and “StartToon” are in scope, of course–your snippet doesn’t reveal much of the structure of your program, including what method it comes from, and I note that both lack a prefixed “self.”…)

(As for what you’re making, it looks like something related to ToonTown; since I’ve never paid it much attention beyond the various resurrection projects that pop up occasionally I’m not in much of a position to guess further, I’m afraid, other than to note that it appears to be part of something along the lines of an avatar-creation interface.)

Thanks, it works perfectly. As for what the program is, it’s currently a Toon Maker, so you’re pretty much right.

nice post good :stuck_out_tongue: