您在tkinter中使用哪个小部件作为Excel表?

时间:2011-08-26 18:19:22

标签: python tkinter widget tktable

我想在tkinter中使用像Excel一样的Excel小部件,以便我正在编写一个gui。你有什么建议吗?

2 个答案:

答案 0 :(得分:10)

您可以像这样使用Tkinter制作一个简单的电子表格,如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()

编辑: 如果您想从网格中获取值,则必须使用网格的子项。

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

你可以在哪里调用该函数,它将返回孩子。要获取条目的值,您可以使用:

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

答案 1 :(得分:7)

如果你需要全桌支持,

Tktable至少可以说是最好的选择。简而言之,以下示例显示了如何使用它,假设您已安装它。示例适用于python3,但对于python2,您只需要更改import语句。

import tkinter as tk
import tktable

root = tk.Tk()
table = tktable.Table(root, rows=10, cols=4)
table.pack(side="top", fill="both", expand=True)
root.mainloop()

由于没有可安装pip的软件包,因此Tktable可能很难安装。

如果您真正需要的是用于显示和编辑数据的小部件网格,则可以轻松构建条目网格或标签小部件。有关示例,请参阅问题this answer

Python. GUI(input and output matrices)?