Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么向tkFileDialog.asksaveasfilename提供initialfile参数会取消选择列表框元素? 问题_Python_Python 2.7_Tkinter - Fatal编程技术网

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)