Search code examples
pythontkinter

How do I set a button to two different commands?


I need the button to change both the background of the main window and the secondary window (called label). I have seen ways to make buttons do 2 different commands, however, I have not seen ways to make a button do two different commands while the command has a variable in the parentheses after the command. Ex. "def command(abc)" <---- here. code below

def color_press(col):
    window.configure(bg='#'+col)
    

def color_pressLAB(labelcol):
    window.configure(bg='#'+labelcol)

and then the button code is:

yellowButton = Button(frame, text='yellow', height=2, width=5, font=25, background='#F5E1A2',
                      command=lambda: color_press('F5E1A2'),
                      command=lambda: color_pressLAB('FFFFBF')

I have only been able to make them change to the same color, but I want the windows to be different colors.


Solution

  • Description:

    The answer is very simple you just have to make use of multiple function usability of lambda in your code. But the way you are using is incorrect instead try this.

    Solution:

    def color_press(col):
        window.configure(bg='#'+col)
        
    def color_pressLAB(labelcol):
        window.configure(bg='#'+labelcol)
    
    yellowButton = Button(frame, text='yellow', height=2, width=5, font=25, background='#F5E1A2',
                          command=lambda: (color_press('F5E1A2'),color_pressLAB('FFFFBF'))