I am running this in Pycharm, and I can't get this dropdown to open on top of other windows. I would also like to center the dropdown menu in the center of the screen.
from tkinter import *
def dropdown_menu(menu_list):
root = Tk()
root.title('Process Mode')
# Create a Tkinter variable
tkvar = StringVar(root)
def on_selection(value):
global dropdown_choice
dropdown_choice = value
root.destroy()
popupMenu = OptionMenu(root, tkvar, *menu_list, command=on_selection)
Label(root, text='Choose Mode').grid(row=2, column=2)
popupMenu.grid(row=2, column=2)
popupMenu.config(width=20, height=2)
# on change dropdown value
def change_dropdown(*args):
global dropdown
dropdown = str(tkvar.get())
# link function to change dropdown
tkvar.trace('w', change_dropdown)
root.mainloop()
return dropdown_choice
options = ['1', '2', '3']
selection = dropdown_menu(options)
You can make the window the topmost window using .attributes()
and place the window at the center of screen using TCL command PlaceWindow
:
def dropdown_menu(menu_list):
...
# place the window at the center of screen
root.call("::tk::PlaceWindow", root._w, "center")
# make the window the topmost window
root.attributes("-topmost", 1)
root.mainloop()
...