Created
April 8, 2021 01:42
-
-
Save israel-dryer/a395a8805f0cb6fe32a1bd83f2fd2807 to your computer and use it in GitHub Desktop.
tkinter startup tearoff menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter | |
def tearoff_callback(menu, tearoff): | |
"""Move the tearoff menu after tearoff""" | |
root.update() | |
x = root.winfo_x() - 75 | |
y = root.winfo_y() | |
root.tk.call('wm', 'geometry', tearoff, f'+{x}+{y}') | |
root = tkinter.Tk() | |
root.geometry('400x400+200+200') | |
tkinter.Label(root, text='This is in the main window').pack() | |
# tearoff menu | |
tearoff_menu = tkinter.Menu(root, tearoff=1, title='File') | |
tearoff_menu.configure(tearoffcommand=tearoff_callback) | |
tearoff_menu.add_command(label="New", command=lambda: print("new")) | |
tearoff_menu.add_command(label="Open", command=lambda: print("open")) | |
tearoff_menu.add_command(label="Save", command=lambda: print("save")) | |
tearoff_menu.add_command(label="Save as...", command=lambda: print("save as...")) | |
tearoff_menu.add_command(label="Close", command=root.quit) | |
# invoke the tearoff callback @ index 0 | |
tearoff_menu.invoke(0) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment