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
  Using Tkinter inside function not working Ensaimadeta 5 4,860 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 3,742 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,736 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,418 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,920 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [PyQt] How to Copy-Paste a table from Office apps to QTableView? Vittorio 5 7,137 Aug-05-2021, 11:14 AM
Last Post: Axel_Erfurt
  tkinter get function finndude 2 2,890 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,234 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  [PyGUI] Python Application Not Finding Excel File dseals26 2 2,787 Feb-17-2021, 01:45 AM
Last Post: thewolf
Bug [PyQt] Qt app won't crash after sys.excepthook override Alfalfa 3 2,614 Dec-28-2020, 01:55 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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