user888469
user888469

Reputation: 137

multiple commands on button press

How would I change this code so that I can also have it start a function called Drop_down_menu()

done_btn = Button(root, text = "Done", command = lambda: root.destroy())       
done_btn.pack()

I have looked at previous articles which say use a function and have the operations there but then it says root is not defined.

Upvotes: 0

Views: 3329

Answers (4)

user15068362
user15068362

Reputation:

for call multipe functions or commands you need to use lambda like this:

test_button = Button(text="your_text_button", command=lambda:[function1(),function2()])
text_button.pack()

Upvotes: 0

Ethan Field
Ethan Field

Reputation: 4740

You could also do this in an object orientated manner, which would clean up your code some by letting you dodge the use of lambda:

from tkinter import *

class App:
    def __init__(self, root):
        self.root = root
        self.btn = Button(self.root, text="Done", command=self.command)
        self.btn.pack()
    def command(self):
        self.root.destroy()
        print("Output")

root = Tk()
app = App(root)
root.mainloop()

Upvotes: 0

user8485449
user8485449

Reputation: 1

try this one

done_btn = Button(root, text = "Done", command = lambda: [root.destroy(),
                                                          Drop_down_menu()])       
done_btn.pack()

hope this answer your question

Upvotes: 0

Mohamed Ali JAMAOUI
Mohamed Ali JAMAOUI

Reputation: 14699

You need to create a function and pass root as a variable to it:

def myfunction(root):
    root.destroy()
    Drop_down_menu()

done_btn = Button(root, text = "Done", command = lambda: myfunction(root))       
done_btn.pack()

For more details on how to use callbacks in Tkinter here's a good tutorial. Here's an example from that tutorial on how to use a callback with a parameter:

def callback(number):
    print "button", number

Button(text="one",   command=lambda: callback(1)).pack()
Button(text="two",   command=lambda: callback(2)).pack()
Button(text="three", command=lambda: callback(3)).pack()  

Hope this helps.

Upvotes: 1

Related Questions