Python Forum
[Tkinter] Override the paste function(copy from excel file - paste in separate tkinter entryes) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Override the paste function(copy from excel file - paste in separate tkinter entryes) (/thread-27272.html)



Override the paste function(copy from excel file - paste in separate tkinter entryes) - AndreiV - Jun-01-2020

If I`m copying the data from a excel file (as exemple 2x rows and 2x columns, or more) and paste them in a tkinter table interface, everything will be pasted in a single entry. The excel cells wil not be spread in tkinter entries (ex: excel A1, A2, B1, B2 are going in the same entry and the point is to make them go in different entries: A1 cell data to be paste in mouse selected row * col; A2 cell data to be paste in mouse selected row + 1 * col + 1; B1 on row + 2 * col + 1 and B2 on row + 2 * col + 2).

I'm wondering if anyone had the same difficulty and if a solution was found.

An exemple bellow:

try:
    from tkinter import * 
except ImportError:
    from Tkinter import *
"""    
import pyperclip

a = pyperclip.paste()
print(a)
space = 0
tabs = 0
newline = 0

first = ""

for character in a:
    if character == " ":
        space +=1
    elif character == '\t': 
        tabs += 1
        print(first)
    elif character == '\n': 
        newline += 1
print(space)
print(tabs)
print(newline)
"""
root = Tk()

my_entries = [] #list for storing entries

height = 10
width = 5
for r in range(10): #numbers of rows
    for c in range(14): #number of columns
        my_entry = Entry(root, width=14)
        my_entry.grid(row=r+2, column=c, pady=1, padx=1, ipady=4)
        my_entries.append(my_entry)
        
root.mainloop()



RE: Override the paste function(copy from excel file - paste in separate tkinter entryes) - AndreiV - Jun-04-2020

This is an example of what I'm trying to copy from excel. A1, A2, A3, B1, B3, B3 are the excel cells.

BUHLMANN 2280092012 2020-04-30
BUHLMANN 2280092014 2020-04-30

"
A1=BUHLMANN A2=2280092012 A3=2020-04-30
B1=BUHLMANN B2=2280092014 B3=2020-04-30
"

I managed to do something like this but not working:
try:
    from tkinter import *
    import tkinter as tk
except ImportError:
    from Tkinter import *
import pyperclip

root = Tk()
root.title("Tolerance changes requests")

copy_clip = pyperclip.paste()
print(copy_clip)
space = 0
tabs = 0
newline = 0

for character in copy_clip:
    if character == " ":
        space +=1
    elif character == '\t':
        tabs += 1
        print(first)
    elif character == '\n':
        newline += 1
print(space)
print(tabs)
print(newline)

entrys = []

for r in range(10):
    for c in range(4):
        entry = Entry(root)
        entry.grid(row=r+2, column=c, pady=1, padx=1, ipady=4)
        entrys.append(entry)

#handle paste
def handle_clipboard(event):
    for entry in entrys:
        entry.delete(0, "end")

    lines = root.clipboard_get().split("\n")
    print(lines)
    rows = root.clipboard_get().split("\t")
    print(rows)
    for entry, row in zip(entrys, rows):
        entry.insert(0, row)

    for entry, line in zip(entrys, lines):
        entry.insert(0, line)
    return "break"

root.bind_all("<<Paste>>", handle_clipboard)

root.mainloop()



RE: Override the paste function(copy from excel file - paste in separate tkinter entryes) - deanhystad - Jun-05-2020

from tkinter import *
 
def create_table(window, rows, columns):
    table = []
    for r in range(rows):
        row = []
        for c in range(columns):
            var   = StringVar()
            entry = Entry(window, textvar=var)
            entry.grid(row=r, column=c, pady=1, padx=1, ipady=4)
            row.append(var)
        table.append(row)
    return table

def paste(event):
    rows = root.clipboard_get().split('\n')
    for r, row in enumerate(rows):
        values = row.split('\t')
        for c, value in enumerate(values):
            table[r][c].set(value)
 
root = Tk()
root.title("Tolerance changes requests")
root.bind_all("<<Paste>>", paste)
table = create_table(root, 10, 4)
 
root.mainloop()
Now you have to figure out what entry was clicked to get the starting row and column for the paste.

There are probably Tkinter table widgets. Have you looked?


RE: Override the paste function(copy from excel file - paste in separate tkinter entryes) - AndreiV - Jun-05-2020

(Jun-05-2020, 02:16 AM)deanhystad Wrote:
from tkinter import *
 
def create_table(window, rows, columns):
    table = []
    for r in range(rows):
        row = []
        for c in range(columns):
            var   = StringVar()
            entry = Entry(window, textvar=var)
            entry.grid(row=r, column=c, pady=1, padx=1, ipady=4)
            row.append(var)
        table.append(row)
    return table

def paste(event):
    rows = root.clipboard_get().split('\n')
    for r, row in enumerate(rows):
        values = row.split('\t')
        for c, value in enumerate(values):
            table[r][c].set(value)
 
root = Tk()
root.title("Tolerance changes requests")
root.bind_all("<<Paste>>", paste)
table = create_table(root, 10, 4)
 
root.mainloop()
Now you have to figure out what entry was clicked to get the starting row and column for the paste.

There are probably Tkinter table widgets. Have you looked?

Thank you very much,
Very elegant way to do it and it's a great step ahead for me, I'm stuck here for some time.

I'm away for a few days but I will try to complete the code when I will be back. The clicked entry can be identified as root.focus_get().

About Tkinter widgets, I've tried other ways but this one seems to be the closed to my needs.