Search code examples
python-3.xtkintertkinter-entry

How can i make a group of entries in python tkinter work like an excel sheet by pressing enter and tab to move cells


Please how can I make the entries I've created work like an Excel sheet, so that when I press enter it goes to the next cell (entry) along that column or when I press tab it's takes me to the next cell (entry) along that row.


Solution

  • import tkinter as tk
    
    from tkinter import *
    
    
    class Example():
    
        def __init__(self):
            self.root = tk.Tk()
            self.table = tk.Frame(self.root)
            self.table.pack(fill="both", expand=True)
    
            name = Label(self.table, font=("Helvetica", 10, "bold"), text='Company').grid(row=0, column=0,sticky=W)
            year1 = Label(self.table, font=("Helvetica", 10, "bold"), text='1999').grid(row=0, column=1,sticky=E)
            year2 = Label(self.table, font=("Helvetica", 10, "bold"), text='2000').grid(row=0, column=2,sticky=E)
            year3 = Label(self.table, font=("Helvetica", 10, "bold"), text='2001').grid(row=0, column=3,sticky=E)
    
            # Setting values for the first column as string variables
            me1 = StringVar()
            me2 = StringVar()
            me3 = StringVar()
            me4 = StringVar()
            me5 = StringVar()
            me6 = StringVar()
            me7 = StringVar()
    
            # Setting values for the second column as float variables
            dwe1 = DoubleVar()
            dwe2 = DoubleVar()
            dwe3 = DoubleVar()
            dwe4 = DoubleVar()
            dwe5 = DoubleVar()
            dwe6 = DoubleVar()
            dwe7 = DoubleVar()
    
            # Setting values for the third column as float variables
            fpe1 = DoubleVar()
            fpe2 = DoubleVar()
            fpe3 = DoubleVar()
            fpe4 = DoubleVar()
            fpe5 = DoubleVar()
            fpe6 = DoubleVar()
            fpe7 = DoubleVar()
    
            # Setting values for the fourth column as float variables
            bfe1 = DoubleVar()
            bfe2 = DoubleVar()
            bfe3 = DoubleVar()
            bfe4 = DoubleVar()
            bfe5 = DoubleVar()
            bfe6 = DoubleVar()
            bfe7 = DoubleVar()
    
            # Putting each of the values in a list for easy calling latter
            rt = [0,[me1,dwe1,fpe1,bfe1],[me2,dwe2,fpe2,bfe2],[me3,dwe3,fpe3,bfe3],[me4,dwe4,fpe4,bfe4],[me5,dwe5,fpe5,bfe5],[me6,dwe6,fpe6,bfe6],[me7,dwe7,fpe7,bfe7]]
    
    
            self.rows = []
            for row in range(1,8):
                row_entries = []
                self.rows.append(row_entries)
                for column in range(4):
                    entry = tk.Entry(self.table,width=10, textvariable=rt[row][column], bd=4, bg='white')
                    entry.grid(row=row, column=column)
                    row_entries.append(entry)
    
                    entry.bind("<Return>", self.handle_enter)
    
        def handle_enter(self, event):
            # get the row and column of the entry that got the event
            entry = event.widget
            row = int(entry.grid_info()['row'])
            column = int(entry.grid_info()['column'])
    
            # compute the new row; either the next row or circle
            # back around to the first row
            new_row = row if row < len(self.rows) else 0
    
            # get the entry for the new row, and set focus to it
            entry = self.rows[new_row][column]
            entry.focus_set()
    
    example = Example()
    
    tk.mainloop()
    

    This is how it looks