您在 tkinter 中使用哪个小部件来创建类似于 Excel 的表格?

发布于 2024-12-01 18:23:21 字数 61 浏览 2 评论 0 原文

我想要一个像 Excel 一样的 tkinter 中的表格小部件,用于我正在编写的 gui。您有什么建议吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小糖芽 2024-12-08 18:23:21

您可以使用 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 child in frame.children.values():
        info = child.grid_info()
        if info['row'] == row and info['column'] == column:
            return child
    return None

该函数将返回孩子。要获取条目的值,您可以使用:

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

注意:在旧版本的 Tkinter 中,行和列存储为字符串,因此您需要转换整数:

if info['row'] == str(row) and info['column'] == str(column):

You can use Tkinter 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()

If you want to get the values from the grid, you can use the grid's children.

def find_in_grid(frame, row, column):
    for child in frame.children.values():
        info = child.grid_info()
        if info['row'] == row and info['column'] == column:
            return child
    return None

The function will return the child. To get the value of the entry, you can use:

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

Note: In old versions of Tkinter, row and column are stored as strings, so there you'd need to cast the integers:

if info['row'] == str(row) and info['column'] == str(column):
遮了一弯 2024-12-08 18:23:21

如果您需要完整的表支持,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()

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

如果您真正需要的是用于显示和编辑数据的小部件网格,则可以轻松构建条目或标签小部件网格。有关示例,请参阅问题此答案 /7432">Python。 GUI(输入和输出矩阵)?

Tktable is at least arguably the best option, if you need full table support. Briefly, the following example shows how to use it assuming you have it installed. The example is for python3, but for python2 you only need to change the import statement.

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()

Tktable can be difficult to install since there is no pip-installable package.

If all you really need is a grid of widgets for displaying and editing data, you can easily build a grid of entry or label widgets. For an example, see this answer to the question Python. GUI(input and output matrices)?

半仙 2024-12-08 18:23:21

我在尝试使用 tkinter 显示完整的 panda 的 DataFrame 时遇到了类似的问题。
我的第一个选择是创建一个网格,就像第一个答案中某人建议的那样,但它对于这么多信息来说太重了。

我想最好创建有限数量的单元并仅显示 DF 的一部分,并随着用户的移动更改该部分

I had a similar problem trying to show a complete panda's DataFrame using tkinter.
My first option was to create a grid like suggested someone in the first answer, but it bacame too heavy for so many info.

I guess is beter to create a finite numbers of cells and show only a part of the DF, changing that part with the movement of the user

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文