1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] how to dataframe data fill to grid entry tkinter python

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024 às 10:02.

  1. Stack

    Stack Membro Participativo

    Sir I would like to share my code that populates testing data into a grid entry. However, I am seeking assistance in using my DataFrame data for this purpose. My current implementation, which utilizes the original DataFrame to fill the grid entry, runs into an error. I would greatly appreciate your guidance on how to effectively use my DataFrame data in the grid entry. Your help would be invaluable to me.

    import tkinter as tk
    import pandas as pd
    #import tkinter

    class ResizableGrid(tk.Frame):
    def __init__(self, parent, rows, columns, *args, **kwargs):
    tk.Frame.__init__(self, parent, *args, **kwargs)

    self.rows = rows
    self.columns = columns

    self.entry_grid = [[tk.Entry(self) for _ in range(columns)] for _ in range(rows)]
    self.configure_grid() # it give error

    self.bind_mouse()

    def configure_grid(self):
    df = pd.read_excel('c:/erp/book11.xlsx',header=None)
    #for i, row in df.iterrows(): # run ok
    # c = 0
    #for cell in row:
    # tk.Label(root, text=cell).grid(row=i, column=c) # run ok fill my df data into lable
    # c += 1


    #for i in range(self.rows):
    for i, row in df.iterrows():
    # for j in range(self.columns): 3 it run gine with out error with testing data
    j = 0
    for cell in row: # it give the error
    self.entry_grid[j].grid(row=i, column=j, sticky="nsew", padx=1, pady=1) # it give error when use dataframe
    #self.entry_grid[j].insert(0, 'testing') # it run fine
    self.entry_grid[j].insert(0, cell) # it give error
    if i==3 and j==0:self.entry_grid[j].config(bg = "green")
    #if i==3 and j==0:self.entry_grid[j].config(bg = "green",width=100)
    self.entry_grid[j].bind("<Enter>", lambda event, col=j: self.on_enter(event, col))
    j += 1




    for i in range(self.rows):
    self.grid_rowconfigure(i, weight=1)

    for j in range(self.columns):
    self.grid_columnconfigure(j, weight=1)

    def bind_mouse(self):
    self.bind("<B1-Motion>", self.on_motion)
    self.bind("<ButtonRelease-1>", self.on_release)

    def on_enter(self, event, col):
    self.current_column = col

    def on_motion(self, event):
    if hasattr(self, "current_column"):
    col = self.current_column
    width = event.x - self.entry_grid[0][col].winfo_x()
    self.columnconfigure(col, minsize=max(50, width))

    def on_release(self, event):
    if hasattr(self, "current_column"):
    delattr(self, "current_column")

    if __name__ == "__main__":
    root = tk.Tk()
    root.title("Resizable Grid")

    rows = 5
    columns = 8

    grid = ResizableGrid(root, rows, columns)
    grid.pack(expand=True, fill="both")

    #button5= tk.Button(root, text="Muhammad")
    #button5.grid(row=2,column=2)
    root.mainloop()



    see error =====================


    File "c:\mainfol\mfatt\11024gridcolumnriszing.py", line 13, in __init__
    self.configure_grid() # it give error
    ^^^^^^^^^^^^^^^^^^^^^
    File "c:\mainfol\mfatt\11024gridcolumnriszing.py", line 32, in configure_grid
    self.entry_grid[j].grid(row=i, column=j, sticky="nsew", padx=1, pady=1) # it give error when use dataframe
    ~~~~~~~~~~~~~~~~~~^^^
    IndexError: list index out of range




    File "c:\mainfol\mfatt\11024gridcolumnriszing.py", line 13, in __init__
    self.configure_grid() # it give error
    ^^^^^^^^^^^^^^^^^^^^^
    File "c:\mainfol\mfatt\11024gridcolumnriszing.py", line 32, in configure_grid
    self.entry_grid[j].grid(row=i, column=j, sticky="nsew", padx=1, pady=1) # it give error when use dataframe
    ~~~~~~~~~~~~~~~~~~^^^
    IndexError: list index out of range

    Continue reading...

Compartilhe esta Página