问题描述
我正在Tkinter中使用 TopLevel
窗口编写文本编辑器,该窗口包含 Text
小部件。当前,当文档/缓冲区包含未保存的更改时,我在窗口的标题前加上星号,如 MyDocument
-> * MyDocument
,这是* nix环境下的惯例。为此,我使用 Text
的 edit_modified
方法,如下所示:
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))
我的问题是:在Mac OS X上,窗口关闭按钮(左上角的红色圆形按钮)中出现黑点,而不是在星号前加星号,表示未保存变化。是否可以从Tkinter(或更常见的是从Tcl / Tk)访问此功能?
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)?
编辑2:
在最初提出使用applescript的建议后,凯文·沃尔泽(Kevin Walzer)提出了解决方案:设置 tkinter
的 wm_attributes
。上面,等于使用
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)
和
self.wm_attributes("-modified", 0) # unsets black dot in toplevel's close button (mac)
在 self.modified
中。
推荐答案
是的,可以使用wm_attributes并将 modified标志设置为true来完成。
Yes, this can be done using wm_attributes and setting the "modified" flag to true.
示例:
from Tkinter import *
root= Tk();
Label(root,text='This is the Toplevel').pack(pady=10)
root.wm_attributes("-modified", 1)
root.mainloop()
这篇关于通过在窗口标题前加*来表示未保存的更改-如何在Mac OS X的窗口关闭按钮中添加黑点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!