Skip to content

Add a "copy to clipboard" function for results #17

@hochwasser

Description

@hochwasser

Hello,

it would be nice to copy the results from a query into the clipboard.
I made an attempt with the little knowledge of tk:
After defining the fw_Box with the result

fw_Box = Treeview(f2, columns=tree_columns, show="headings", padding=(2, 2, 2, 2))

attach a mouse handler that opens a popup menu

e = None

def cell_copy():
    global e
    citem = fw_Box.selection()[0]
    column = fw_Box.identify_column(e.x)
    column_no = int(column.replace("#", "")) - 1

    fw_Box.clipboard_clear()
    fw_Box.clipboard_append(fw_Box.item(citem)["values"][column_no])

def all_copy():
    # Kopf
    clipstr = "|"
    for col in fw_Box['columns']:
        clipstr += f"{col}|"
    clipstr += "\n"

    # Zeilen
    for line in fw_Box.get_children():
        clipstr += "|"
        for value in fw_Box.item(line)['values']:
            clipstr += f"{value}|"
        clipstr += "\n"

    fw_Box.clipboard_clear()
    fw_Box.clipboard_append(clipstr)

def popup_menu(event):
    global e
    e = event
    fw_Box.identify_row(event.y)
    popup1.post(event.x_root, event.y_root)

popup1 = Menu(fw_Box, tearoff=0)
popup1.add_command(
    command=cell_copy,
    label="Copy cell")
popup1.add_command(
    command=all_copy,
    label="Copy all")

fw_Box.bind('<Button-3>', popup_menu)

This code is more a prove of concept.
For example: I didn't figure out howto pass the variable event to the copy function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions