Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x 如何选择所有treeview并使用ctrl-c复制它_Python 3.x_Tkinter_Treeview - Fatal编程技术网

Python 3.x 如何选择所有treeview并使用ctrl-c复制它

Python 3.x 如何选择所有treeview并使用ctrl-c复制它,python-3.x,tkinter,treeview,Python 3.x,Tkinter,Treeview,1-如何选择所有树视图,ctrl+a并用ctrl-c复制,我找到了如何选择多行 2-是否可以将选择粘贴为数组,而不是文本,例如,当我在Excel中粘贴选择时,它将位于多个列中 import tkinter as tk from tkinter import ttk root = tk.Tk() tree = ttk.Treeview(root) tree.pack() tree['columns'] = ('one', 'two', 'three') tree.column('#0', wi

1-如何选择所有树视图,ctrl+a并用ctrl-c复制,我找到了如何选择多行

2-是否可以将选择粘贴为数组,而不是文本,例如,当我在Excel中粘贴选择时,它将位于多个列中

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
tree = ttk.Treeview(root)
tree.pack()

tree['columns'] = ('one', 'two', 'three')
tree.column('#0', width=170, stretch=tk.NO)
tree.column('one', width=100, stretch=tk.NO)
tree.column('two', width=100, stretch=tk.NO)
tree.column('three', width=180, stretch=tk.NO)

tree.heading('#0', text='Name', anchor=tk.W)
tree.heading('one', text='Col1', anchor=tk.W)
tree.heading('two', text='Col2', anchor=tk.W)
tree.heading('three', text='Col3', anchor=tk.W)

tree.insert('', 'end', text='The First Item Name',values=('Col1_1', 'Col2_1', 'https://test0.test'))
tree.insert('', 'end', text='The Second Item Name',values=('Col1_2', 'Col2_2', 'https://test1.test'))
tree.insert('', 'end', text='The Third Item Name',values=('Col1_3', 'Col2_3', 'https://test2.test'))

tree.bind('<Control-a>', lambda *args: tree.selection_add(tree.get_children())) #selected all row treeview

root.mainloop()
将Ctrl+C绑定到一个复制函数,该函数检索选定行的值并将其放入剪贴板。为此,您可以使用: tree.selection以获取所选行IID tree.item“text”以获取第一列的内容 tree.item“values”以获取其他列的内容 root.clipboard\u清除以清除剪贴板 root.clipboard\u append可将文本追加到剪贴板 LibreOffice所以我猜Excel也是如此,但我还没有尝试过在不同的列中粘贴由制表符分隔的内容,我认为可以使用其他类型的分隔符。因此,我们的想法是用制表符分隔一行的值,用换行符分隔行。 更新:要复制标题,可以使用tree.heading检索标题属性,因此

提供所有列标题的列表

复制功能的完整代码为:

def copy(event):
    sel = tree.selection() # get selected items
    root.clipboard_clear()  # clear clipboard
    # copy headers
    headings = [tree.heading("#{}".format(i), "text") for i in range(len(tree.cget("columns")) + 1)]
    root.clipboard_append("\t".join(headings) + "\n")
    for item in sel:
        # retrieve the values of the row
        values = [tree.item(item, 'text')]
        values.extend(tree.item(item, 'values'))
        # append the values separated by \t to the clipboard
        root.clipboard_append("\t".join(values) + "\n")

tree.bind('<Control-c>', copy)

请告诉我们你试过什么。你知道如何用tkinter制作键盘绑定吗?如果没有,请先看一些教程和示例。此外,还不清楚将所选内容粘贴为数组是什么意思,因此,如果您想获得答案,请更加精确并举例说明。您可能还想看一下。@j_4321:对不起,我更改了问题,我希望我已经足够清楚了,谢谢您的收听。它碰巧运行得很好,谢谢您提供的详细信息。它适用于我的第二个问题:请回答最后一个问题,如何用选择复制标题感谢一百万
def copy(event):
    sel = tree.selection() # get selected items
    root.clipboard_clear()  # clear clipboard
    # copy headers
    headings = [tree.heading("#{}".format(i), "text") for i in range(len(tree.cget("columns")) + 1)]
    root.clipboard_append("\t".join(headings) + "\n")
    for item in sel:
        # retrieve the values of the row
        values = [tree.item(item, 'text')]
        values.extend(tree.item(item, 'values'))
        # append the values separated by \t to the clipboard
        root.clipboard_append("\t".join(values) + "\n")

tree.bind('<Control-c>', copy)