簡體   English   中英

從 treeview tkinter 復制項目

[英]Copy items from treeview tkinter

我的 tkinter 應用程序中有一個樹視圖,我想知道是否真的有可能,只需在用戶右鍵單擊時復制選定的字段。 如果沒有,是否有任何其他小部件允許用戶復制 GUI window 中顯示的選定字段。

代碼:

    log = Toplevel(root)
    log.title('View all Visitors')
    log.focus_force()
    # setup treeview
    columns = (('ID', 80), ('S_ID', 80), ('S_NAME', 300), ('Title of the book', 500), ('Accession no. of 
                book', 80),
               ('Date Taken', 100), ('Due Date', 100), ('Date_Returned', 100), ('Status', 80))
    tree = ttk.Treeview(log, height=20, columns=[
                        x[0] for x in columns], show='headings')
    tree.grid(row=0, column=0, sticky='news')

    # setup columns attributes
    for col, width in columns:
        tree.heading(col, text=col)
        tree.column(col, width=width, anchor=tk.CENTER)

    # fetch data
    con = mysql.connect(host='localhost', user='root',
                        password='monkey123', database='library')
    c = con.cursor()
    sql_command_1 = 'SELECT * FROM borrow;'
    c.execute(sql_command_1)

    # populate data to treeview
    for rec in c:
        tree.insert('', 'end', value=rec)

    # scrollbar
    sb = tk.Scrollbar(log, orient=tk.VERTICAL, command=tree.yview)
    sb.grid(row=0, column=1, sticky='ns')
    tree.config(yscrollcommand=sb.set)
    a = tree.item(tree.focus())['values']

    btn = tk.Button(log, text='Close', command=out,
                    width=20, bd=2, fg='red',font=font_text)
    btn.grid(row=2, column=0, columnspan=2, sticky=E+W)

提前致謝:)

您必須創建一個彈出菜單並將其綁定到 Button-3。 這是從我的一個項目中快速構建的示例

popup1 = tk.Menu(tree, tearoff=0)
popup1.add_command(
    command=your_copy,
    label="Copy")

def your_copy():
    item = tree.selection()[0]
    log.clipboard_clear()
    log.clipboard_append(tree.item(item, option='text')

def popup_menu(event):
    tree.identify_row(event.y)
    popup1.post(event.x_root, event.y_root)
    
tree.bind('<Button-3>', popup_menu)

我遇到了同樣的問題,並創建了一個非常模塊化的 function。

import pyperclip    

def copy_from_treeview(tree, event):
    selection = tree.selection()
    column = tree.identify_column(event.x)
    column_no = int(column.replace("#", "")) - 1
            
    copy_values = []
    for each in selection:
        try:
            value = tree.item(each)["values"][column_no]
            copy_values.append(str(value))
        except:
            pass
        
    copy_string = "\n".join(copy_values)
    pyperclip.copy(copy_string)

只需將 function 綁定到 ctrl + c:

tree.bind("<Control-Key-c>", lambda x: copy_from_treeview(tree, x))

您可以嘗試獲取選定的行,然后從那里開始工作:

import pyperclip

treeview.bind("<Control-Key-c>", copy_from_treeview)

def copy_from_treeview(treeview, _):
    selections = treeview.selection()  # get hold of selected rows

    copied_string = ""
    for row in selections:
        values = treeview.item(row, 'values')  # get values for each selected row

        for item in values:
            copied_string += f"{item}  "

    pyperclip.copy(copied_string)

   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM