[Python] How to fill data in multiple grid entry tkinter

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    boss

    I observe that my code executes correctly and successfully resizes the grid column; however, the cell remains unpopulated. I would like to populate the cell or entry box with data from my database, Excel file, or data frame. Additionally, I seek guidance on how to fill the cell based on specific row or column numbers.

    see my code

    import tkinter as tk

    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,cursor="circle") for _ in range(columns)] for _ in range(rows)]
    # self.entry_grid.insert(0, "Hello, world") # how to fill grid entry by my databse data or fill by row and column
    # $self.entry_grid.insert(0, rows+colmun)
    self.configure_grid()

    self.bind_mouse()

    def configure_grid(self):
    for i in range(self.rows):
    for j in range(self.columns):
    self.entry_grid[j].grid(row=i, column=j,sticky="nsew", padx=1, pady=1)
    self.entry_grid[j].bind("<Enter>", lambda event, col=j: self.on_enter(event, col))

    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")
    root.mainloop()


    self.entry_grid.insert(0, "Hello, world")

    Continue reading...

Compartilhe esta Página