0

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.

2
  • About which "previous articles" do you say? This is code using module TK? Be more specific Commented Aug 18, 2017 at 15:21
  • 2
    Show your code, and the resulting error messages. Commented Aug 18, 2017 at 15:21

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

this error comes up: TypeError: <lambda>() missing 1 required positional argument: 'root'
@user8469209 Try Button(root, text = "Done", command = lambda: myfunction(root)) so the command is a function of no parameters.
@AMagoon that would call myfunction(root) immediately, not on Button press
0

try this one

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

hope this answer your question

Comments

0

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()

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.