0
votes

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
In all my years of using a mac I've never noticed a black dot in the close button. What application does that?Bryan Oakley
Well, it seems it depends indeed on the application and the OS X version: TextEdit (mac os x 10.6.8, but not os x 10.7.5?), Emacs, LibreOffice, MSWord (both os x versions)armando.sano
Most OS X apps use this black dot to indicate that the current document has changed (Mail is one example). Starting with Lion, it is not used by apps that instead use the OS-level version control.Paul Lefebvre
No idea if you can call OS functions with tkinter, but you are looking to use IsWindowModified and SetWindowModified, at least on Carbon.Paul Lefebvre
(Just wondering how the Lion os can know about changes in an application's internal buffer?) Thanks for pointing to these functions. Can they be called through applescript? (And would you know how?) For example, one can force the tkinter window to be raised on os x by calling /usr/bin/osascript -e using Python's os.system call: see e.g. fyngyrz.com/?p=898armando.sano

1 Answers

2
votes

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()