Which widget do you use for a Excel like table in tkinter?

前端 未结 2 1417
猫巷女王i
猫巷女王i 2021-02-03 10:38

I want a Excel like table widget in tkinter for a gui I am writing. Do you have any suggestions?

2条回答
  •  心在旅途
    2021-02-03 11:09

    You can use Tkinter like so to make a simple spreadsheet like gui:

    from Tkinter import *
    
    root = Tk()
    
    height = 5
    width = 5
    for i in range(height): #Rows
        for j in range(width): #Columns
            b = Entry(root, text="")
            b.grid(row=i, column=j)
    
    mainloop()
    

    Edit: If you wanted to get the values from the grid, you have to use the grid's children.

    def find_in_grid(frame, row, column):
        for children in frame.children.values():
            info = children.grid_info()
            #note that rows and column numbers are stored as string                                                                         
            if info['row'] == str(row) and info['column'] == str(column):
                return children
        return None
    

    Where you can call the function and it will return the child. To get the value of the entry, you can use:

    find_in_grid(root, i+1, j).get()
    

提交回复
热议问题