Python 为什么向tkFileDialog.asksaveasfilename提供initialfile参数会取消选择列表框元素? 问题
当我向Python 为什么向tkFileDialog.asksaveasfilename提供initialfile参数会取消选择列表框元素? 问题,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,当我向tkFileDialog提供initialfile关键字参数时。asksaveasfilename所有我的Listbox元素都未被选中 MCVE 我正在使用Python2.7.15+和Ubuntu18.04.3 LTS下的Tkinter 8.6版 import Tkinter as tk import tkFileDialog import ttk # set up a box with some selected elements root = tk.Tk() box = tk.Lis
tkFileDialog提供initialfile
关键字参数时。asksaveasfilename
所有我的Listbox
元素都未被选中
MCVE
我正在使用Python2.7.15+和Ubuntu18.04.3 LTS下的Tkinter 8.6版
import Tkinter as tk
import tkFileDialog
import ttk
# set up a box with some selected elements
root = tk.Tk()
box = tk.Listbox(root, selectmode=tk.MULTIPLE)
for s in ('this', 'is a', 'minimal', 'example'):
box.insert(tk.END, s)
box.selection_set(1)
box.selection_set(3)
box.grid()
# hitting this button does not reset the current selection in box
ttk.Button(root, text='no initialfile',
command=tkFileDialog.asksaveasfilename).grid()
# hitting this button resets the current selection in box - why?
ttk.Button(root, text='with initialfile',
command=lambda: tkFileDialog.asksaveasfilename(initialfile='XXX')).grid()
root.mainloop()
结果
这是因为默认情况下一次只能选择一组文本。这是因为tk小部件将选择导出到剪贴板,并且一次只能有一个项目在剪贴板上 通过将列表框上的
exportselection
设置为False
,删除列表框选择与剪贴板之间的连接,可以防止这种情况发生
box = tk.Listbox(root, selectmode=tk.MULTIPLE, exportselection=False)