I have a larger tkinter app that I wanted to dinamically set the topmost attribute. I am able to achieve what I want, but everytime I check the state of topmost, the selected menu bar on the screen goes invisible.
To reproduce this, consider the MRE below and upon running the code, click the "menu" button on the menu bar, the cascade opens and the exit button shows, watch it vanish after the check_topmost function runs, but the "menu" button is still pressed somehow.
Commenting out either the line that checks the attribute or the line that sets it as True stops the behaviour
import tkinter as tk
def check_topmost():
print(app.attributes('-topmost')) # comment this
app.after(1000, check_topmost)
app = tk.Tk()
menu_bar = tk.Menu(app)
sub_menu = tk.Menu(menu_bar, tearoff=0)
sub_menu.add_command(label = 'exit', command = app.destroy)
menu_bar.add_cascade(label = "menu", menu = sub_menu)
app.config(menu = menu_bar)
app.attributes('-topmost', 1) # comment this
app.after(1000, check_topmost)
app.mainloop()
Any hints as to what I'm doing wrong or why is this happening are greatly appreciated!
Please forgive any typos, I wrote this on my phone.
This is not a fix for your issue, but rather a workaround. It seems like the
topmostattribute is really weird... First, it is platform specific. Second, once set, it doesn't just change. If you set"-topmost", 1for another window both will have the same attribute. From the documentation I found"-topmost gets or sets whether this is a topmost window". I assume, that once you call the attribute, it not only checks the attribute, but reassigns the value, therefore giving you the weird behavior with the drop-down menu. Interestingly, if you get all attributes at the same time byapp.attributes(), your drop-down menu is not affected. So you can use this as workaround to check the attribute and only set it if necessary. Most likely, this is not the best solution, but it is the best I could come up with.