• tksheet - Copy and Paste with headers

    From angela vales@21:1/5 to All on Fri Apr 14 11:33:27 2023
    Hello All,

    I found this small group in a google search, so not sure if it is still active but giving it a try nonetheless.

    I have recently created a tkinter app and need the ability to copy and paste data from tksheet table into an Excel file. I do have a button for export, but it will be beneficial to also allow the user to simply copy,paste.

    I have enabled the appropriate bindings but cannot find a solution to also
    copy the header information during the copy and paste.

    My table is generated after a query is run. Here is a small snippet.

    df = pd.read_sql_query(query, conn)
    results_table.set_sheet_data(df.values.tolist())
    results_table.headers(df.columns.tolist())
    results_table.enable_bindings(
    "drag_select",
    "select_all",
    "column_drag_and_drop",
    "row_drag_and_drop",
    "column_select",
    "row_select",
    "arrowkeys",
    "right_click_popup_menu",
    "copy",
    "paste",
    "undo"
    )

    best
    Angela Vales

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jason Wang@21:1/5 to All on Sun Apr 16 18:57:18 2023
    在 2023/4/15 2:33, angela vales 写道:
    Hello All,

    I found this small group in a google search, so not sure if it is still active but giving it a try nonetheless.

    I have recently created a tkinter app and need the ability to copy and paste data from tksheet table into an Excel file. I do have a button for export, but it will be beneficial to also allow the user to simply copy,paste.

    I have enabled the appropriate bindings but cannot find a solution to also copy the header information during the copy and paste.

    My table is generated after a query is run. Here is a small snippet.

    df = pd.read_sql_query(query, conn)
    results_table.set_sheet_data(df.values.tolist())
    results_table.headers(df.columns.tolist())
    results_table.enable_bindings(
    "drag_select",
    "select_all",
    "column_drag_and_drop",
    "row_drag_and_drop",
    "column_select",
    "row_select",
    "arrowkeys",
    "right_click_popup_menu",
    "copy",
    "paste",
    "undo"
    )

    best
    Angela Vales
    I am not sure how to solve your problem. The newsgroup may not be able
    to wait for an answer, so I suggest that you go to Stack Overflow to ask questions after conducting a thorough Google search. Of course, you can
    also wait for someone who knows the answer to come and answer:D

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alan Gauld@21:1/5 to All on Sun Apr 16 14:01:42 2023
    在 2023/4/15 2:33, angela vales 写道:
    I have recently created a tkinter app and need the ability to copy and
    paste data from tksheet table into an Excel file.

    First thanks for drawing my attention to tksheet. I've long
    been desiring a decent table widget in tkinter and was on the
    verge of trying to create one of my own. tksheet looks like
    it will do all I need.

    As to copy/paste I couldn't see any explicit mention but
    it does say the underlying data is in a Tk Canvas so it may
    be that copy/paste will just work, did you try it? What
    happened if you paste into a text editor in the first
    instance? And Excel in the second?

    If all else fails you can probably write handlers and bind
    to Ctrl-C and Ctrl-V to do something yourself that mimics
    cut/paste.


    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Alan Gauld on Sun Apr 16 09:53:03 2023
    On 4/16/2023 9:01 AM, Alan Gauld wrote:

    在 2023/4/15 2:33, angela vales 写道:
    I have recently created a tkinter app and need the ability to copy and
    paste data from tksheet table into an Excel file.

    First thanks for drawing my attention to tksheet. I've long
    been desiring a decent table widget in tkinter and was on the
    verge of trying to create one of my own. tksheet looks like
    it will do all I need.

    As to copy/paste I couldn't see any explicit mention but
    it does say the underlying data is in a Tk Canvas so it may
    be that copy/paste will just work, did you try it? What
    happened if you paste into a text editor in the first
    instance? And Excel in the second?

    If all else fails you can probably write handlers and bind
    to Ctrl-C and Ctrl-V to do something yourself that mimics
    cut/paste.

    I have not used tksheet, but in its documentation at https://github.com/ragardner/tksheet/wiki#14-getting-selected-cells,
    there is the method

    get_currently_selected()

    Returns namedtuple of (row, column, type_) e.g. (0, 0, "column")
    type_ can be "row", "column" or "cell"

    There are companion methods for rows, columns, etc. That would be a
    good starting point.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aapost@21:1/5 to angela vales on Sun Apr 16 10:24:34 2023
    On 4/14/23 14:33, angela vales wrote:
    I have recently created a tkinter app and need the ability to copy and paste data from tksheet table into an Excel file. I do have a button for export, but it will be beneficial to also allow the user to simply copy,paste.

    I have enabled the appropriate bindings but cannot find a solution to also copy the header information during the copy and paste.

    the csv export code runs through a different path than the ctrl_c code,
    one operating on the sheet level, one on the main table level (I didn't
    dig in to the depths but my assumptions would be that main table doesn't mathematically consider the headers in the same way).

    def yield_sheet_rows in _tksheet.py vs def ctrl_c in _tksheet_main_table.py

    Comparing how the each path functions, without a larger redesign of
    tksheet, you could create a custom button press combo binding to
    something other than ctrl-c utilizing the yield_sheet_rows (or -
    disallow all other ctrl-c functionality in favor of ONLY a csv style
    everything dump when using ctrl-c):

    Import these:
    import csv as csv_module
    import io

    This would be your custom binding functionality:
    rows = self.sheet.yield_sheet_rows(get_header = True, get_index = False)

    s = io.StringIO()
    writer = csv_module.writer(s, dialect = csv_module.excel_tab,
    lineterminator = "\n")
    for row in rows:
    writer.writerow(row)
    self.clipboard_clear()
    self.clipboard_append(s.getvalue())

    It would need something deeper if you wanted to integrate it to ctrl-c
    and keep the existing ctrl-c functionality

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)