This is what I have tried:
win = Tk()
menubar = Menu(win)
dropDown = Menu(menubar)
dropDown.add_command(label = "Do something", command = ...)
entry = Entry()
dropDown.add(entry)
menubar.add_cascade(label = "Drop Down", menu = dropDown)
win.config(menu = menubar)
win.update()
I have looked through the docs and it seems like there is no way to do it with a single line like dropDown.add_entry(...)
, but I thought there might be a workaround like using the one of the geometry manager to place the entry in the menu somehow.
I am using Python 3.6 (but I'm not tagging it because I'll get a thousand mods from the Python tag who have no interest in answering my question voting to close for no reason)
Here is a simple program where you can click a menu button to prompt the user for input. Then do something with that input. In this case print to console.
We need to write a function that makes use of askstring()
from simpledialog
that you can import from Tkinter. Then take the results of that string the user types and do something with it.
import tkinter as tk
from tkinter import simpledialog
win = tk.Tk()
win.geometry("100x50")
def take_user_input_for_something():
user_input = simpledialog.askstring("Pop up for user input!", "What do you want to ask the user to input here?")
if user_input != "":
print(user_input)
menubar = tk.Menu(win)
dropDown = tk.Menu(menubar, tearoff = 0)
dropDown.add_command(label = "Do something", command = take_user_input_for_something)
# this entry field is not really needed her.
# however I noticed you did not define this widget correctly
# so I put this in to give you an example.
my_entry = tk.Entry(win)
my_entry.pack()
menubar.add_cascade(label = "Drop Down", menu = dropDown)
win.config(menu = menubar)
win.mainloop()