0

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.

armando.sano
  • 345
  • 1
  • 3
  • 9
  • 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 Apr 06 '13 at 15:54
  • 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 Apr 06 '13 at 16:31
  • 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 Apr 07 '13 at 16:11
  • 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 Apr 07 '13 at 16:12
  • (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. http://fyngyrz.com/?p=898 – armando.sano Apr 07 '13 at 16:32

1 Answers1

2

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()
Kevin Walzer
  • 538
  • 4
  • 14
  • Neat! I didn't know that feature was added. Thanks, Kevin. – Bryan Oakley May 11 '13 at 11:26
  • Fantastic, that works perfectly, thanks so much! Had never come across wm_attributes before, good to know. I've edited the original question to remove any reference to applescript. – armando.sano May 13 '13 at 13:13