Python Forum
[Tkinter] Override the paste function(copy from excel file - paste in separate tkinter entryes)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Override the paste function(copy from excel file - paste in separate tkinter entryes)
#1
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()
Reply
#2
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()
Reply
#3
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?
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug [PyQt] Qt app won't crash after sys.excepthook override Alfalfa 3 182 Dec-28-2020, 01:55 AM
Last Post: deanhystad
  Using Tkinter inside function not working Ensaimadeta 3 195 Dec-23-2020, 02:57 PM
Last Post: deanhystad
  function in new window (tkinter) Dale22 7 346 Nov-24-2020, 11:28 PM
Last Post: Dale22
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 264 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 399 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  tkinter get method is not accepting value when called by function jagasrik 1 287 Sep-16-2020, 05:28 AM
Last Post: Yoriz
  [Tkinter] Python - Tkinter : How can I send path Excel from GUI to scripte johnjh 2 648 Aug-28-2020, 05:38 PM
Last Post: maaad
  [Tkinter] Use function from other class (Tkinter) zarize 8 612 Aug-17-2020, 09:47 AM
Last Post: zarize
  [Tkinter] updating tkinter chart from within function mikisDW 1 370 Jul-02-2020, 03:33 AM
Last Post: deanhystad
  Passing arguments into function, tkinter nanok66 3 745 Apr-18-2020, 11:53 PM
Last Post: nanok66

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020