iI have an tkinter application which has in the window title the full path-name of the file which is loaded into the application. As the path-name is usually long it can only be displayed when the window is not minimized. When it is minimized (to an icon) only the start of the path-name is visible. So I want to switch the title to only the file-name at the moment, the window gets minimized. I found a solution for this here and adapted it to my problem:
import tkinter as tk
title_saved = ""
def __made_to_window(event):
print("__made_to_window")
global title_saved
if title_saved!="":
root.title(title_saved)
title_saved = ""
def __made_to_icon(event):
print("__made_to_icon")
global title_saved
if title_saved=="":
title_saved = root.title()
root.title("filename.py")
root = tk.Tk()
root.title("C/folder1/folder2/folder3/folder4/folder5/filename.py")
canvas = tk.Canvas(root, height=100, width=400)
canvas.grid()
root.bind("<Unmap>", __made_to_icon)
root.bind("<Map>" , __made_to_window)
root.mainloop()
As you can see with my example code, the solution works. But I don't like it as the used binding is not only activated once but 2 times, when the window is minimized (in my big application it is activated 10 times when the window is minimized). Because of this the variable title_saved must be checked, if it is already not empty anymore.
So I am looking for a more elegant solution, especially because I believe changing the title at minimizing must be a common problem.
Do you have any ideas?
When you bind to the root window, that binding is inherited by all windows. The simple solution is to check which window the event is for, and only do the work when the window is the root window.
def __made_to_icon(event):
global title_saved
if event.widget == root:
print("__made_to_icon")
if title_saved=="":
title_saved = root.title()
root.title("filename.py")