Two checkbuttons to toggle sound not working right

33 views Asked by At
    self.mb = Menubutton ( self.window2, text="Sound Toggler", relief=RAISED )
    self.mb.grid(row=4, column = 0)
    self.mb.menu  =  Menu ( self.mb, tearoff = 0 )
    self.mb["menu"]  =  self.mb.menu

    self.ONSound  = IntVar()
    self.ONSound.set(1)
    self.OFFSound = IntVar()
    self.OFFSound.set(0)

    self.mb.menu.add_checkbutton ( label="ONSound", variable=self.ONSound, command = self.turnON(), onvalue=1,offvalue=0)
    self.mb.menu.add_checkbutton ( label="OFFSound", variable=self.OFFSound, command = self.turnOFF(), onvalue=1,offvalue=0)

def turnON(self):
    self.ONSound.set(1)
    self.OFFSound.set(0)

def turnOFF(self):
    self.ONSound.set(0)
    self.OFFSound.set(1)

My goal is to turn these two check buttons to toggle sound On and OFF and in doing so only one of these check buttons can be on/off at a single time. Currently this effect is not working and I've been looking at my code for an hour and can't find the problem. Everything shows up when my full program is run but this ON/OFF toggle doesn't work how I want it to.

All help is appreciated, Thanks

1

There are 1 answers

4
fhdrsdg On BEST ANSWER

The problem is that you set the command to a function call instead of a funciton reference. This makes the functions run once and assign the return value (None) to command. You should remove the parentheses after the function names.


To make two options of which only one can be selected however, why don't you make them radiobuttons? Then it's as simple as

self.Sound  = IntVar()

self.mb.menu.add_radiobutton(label= "ONSound", variable=self.Sound, value=1)
self.mb.menu.add_radiobutton(label="OFFSound", variable=self.Sound, value=0)