Python Tkinter:将小部件添加到文件对话框
我正在将Tkinter
与Python 2.6
和2.7
一起用于编程图形用户界面.
I am using Tkinter
with Python 2.6
and 2.7
for programming graphic user interfaces.
这些用户界面包含用于打开文件和保存tkFileDialog
模块中的数据的对话框.我想调整对话框并添加其他条目小部件,例如让用户发表评论.
These User Interfaces contain dialogs for opening files and saving data from the tkFileDialog
module. I would like to adapt the dialogs and add some further entry widgets e.g. for letting the user leave comments.
有什么办法吗?
似乎文件对话框直接来自操作系统.在Tkinter
中,它们是从tkCommonDialog
模块中的Dialog类派生的,并调用框架小部件的tk.call("tk_getSaveFile")
方法(在这种情况下为保存数据).
It seems that the file dialogs are taken directly from the operating system. In Tkinter
they are derived from the Dialog class in the tkCommonDialog
module and call the tk.call("tk_getSaveFile")
method of a frame widget (in this case for saving data).
我无法确定此方法的定义位置.
I could not find out where this method is defined.
call
method is defined in _tkinter.c
, but there is nothing interesting for your particular task there. It just calls a Tcl command, and the command tk_getSaveFile
does all the work.
是的,当操作系统上存在本机文件对话框时,tk_getSaveFile
会使用它们(例如,在Windows上使用GetSaveFileName
).可以在此处添加小部件,但不能不篡改Tk的C源代码.如果您确定您的目标使用非本机Tk对话框,则可以通过从Tk入侵::tk::dialog::file::
过程(请参见library/tkfbox.tcl
)来向其小部件层次添加一些内容.
And yes, when there is a native file dialog on the operating system, tk_getSaveFile
uses them (e.g. GetSaveFileName
is used on Windows). It could be possible to add widgets there, but not without tampering with C sources of Tk. If you're sure that your target uses non-native Tk dialogs, you could add something to its widget hierarchy by hacking ::tk::dialog::file::
procedure from Tk (see library/tkfbox.tcl
).
我宁愿使用纯Tcl/Tk编写的 tk_getSaveFile的替代实现,并且从不使用操作系统工具.这样,我们可以确保所有操作系统的布局都是相同的,并且不会随着新版本的Tk突然改变.为它周围的python提供便利的API仍然很简单,但是至少有可能.
I would rather take an alternative implementation of tk_getSaveFile, written in pure Tcl/Tk and never using the OS facility. This way, we can be sure that its layout is the same for all OSes, and it won't suddenly change with a new version of Tk. It's still far from trivial to provide a convenient API for python around it, but at least, it is possible.