1

I am facing this issue where whenever I try to trigger the activate() method by clicking the command inside the Menu widget, I am not able to see the method in action because by default the Menu widget will close automatically when I click any of its command.

Here is my code:

import tkinter as tk

def on_open():
    print("Stop1")
    file_menu.activate(1)  # Automatically highlight the 'Save' option
    print("Stop2")

root = tk.Tk()
root.geometry("400x200")

menu_bar = tk.Menu(root)
root.config(menu=menu_bar)

file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)

file_menu.add_command(label="Open", command=on_open)
file_menu.add_command(label="Save", command=lambda: print("Save selected"))
file_menu.add_command(label="Exit", command=root.quit)

root.mainloop()

Does anyone has any idea how to effectively use the activate() of Menu widget class without closing the Menu widget?

2
  • Does this answer your question? Keep a menu open in Tkinter Commented Apr 30, 2024 at 12:28
  • It didn't quite work. The codes basically re-render the Menu widget again which means that the activate() method will reset. I think the activate() method will only work if the Menu widget did not refresh when clicked but since the default trait of Menu widget is to automatically close when one of its command is pressed. I think the activate() method is probably just useless for Menu widget. Commented Apr 30, 2024 at 15:18

0

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.