Signifying unsaved changes by prepending * in window title - how to add a black dot in the window close button on Mac OS X?

跟風遠走 提交于 2019-12-12 06:09:33

问题


I am writing a text editor in Tkinter, using a TopLevel window that includes a Text widget. Currently, when a document/buffer contains unsaved changes, I prepend the title of the window with an asterisk as in MyDocument -> *MyDocument , as customary under *nix environments. For that purpose, I am using the edit_modified method of Text as follows:

import Tkinter as tk
class EditingWindow(tk.Toplevel):
    # [...]

    self.text = tk.Text(...)

    # track modifications of the text:
    self.text.bind("<<Modified>>", self.modified)

    def modified(self, event=None):
        if self.text.edit_modified():
            title=self.title()
            if title[0] != '*':
                self.title("*" + title)
        else:
            title=self.title()
            if title[0] == '*':
                self.title(title[1:])

    def save(self, event=None):
        # [... saving under a filename kept in the variable self.filename ...]
        self.text.edit_modified(False)
        self.title(os.path.basename(self.filename))

My question is: On Mac OS X, rather than prepending the window title with an asterisk, a black dot appears in the window close button (the red circular button on the topleft corner) to signify unsaved changes. Is it possible to access this feature from Tkinter (or, more generally, from Tcl/Tk)?

Edit 2: After initial suggestions to use applescript, Kevin Walzer came up with the solution: setting tkinter's wm_attributes. Above, that amounts to using

self.wm_attributes("-modified", 1) # sets black dot in toplevel's close button (mac)

and

self.wm_attributes("-modified", 0) # unsets black dot in toplevel's close button (mac)

in self.modified.


回答1:


Yes, this can be done using wm_attributes and setting the "modified" flag to true.

Example:

from Tkinter import *
root= Tk();
Label(root,text='This is the Toplevel').pack(pady=10)
root.wm_attributes("-modified", 1)
root.mainloop()


来源:https://stackoverflow.com/questions/15852462/signifying-unsaved-changes-by-prepending-in-window-title-how-to-add-a-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!