Python Tkinter Table适合窗口太多行/列

标签 python tkinter

我正在尝试编写一个代码,它将数据作为表格返回。不幸的是,它返回 12 行和 9 列,尽管我只有 2 个数据点要显示。我的错误在哪里?

这是使用的代码:

Pastebin with code

import Tkinter as tk
from tkintertable.Tables import TableCanvas
class createTable(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        #########################################
        self.master.grid_rowconfigure(0, weight=1)
        self.master.grid_columnconfigure(0, weight=1)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.grid(sticky=tk.NW+tk.SE)
        #########################################
        self.F = tk.Frame(self)
        self.F.grid(row=0, column=0, sticky=tk.NW+tk.SE)
        self.createWidgets()

    def createWidgets(self):
        self.table = TableCanvas(self.F,cellwidth=250, editable=False, )
        self.table.createTableFrame()
        model = self.table.model
        model.importDict(d)
        self.table.redrawTable()

if __name__ == '__main__':
    f = open("./out.txt")
    tempDic = {}

    for var, line in enumerate(f):
        if not "\t" in line and line != '\n':
            key = line.replace(":","").strip()
            tempDic[key] = []
        elif line == '\n':
            continue
        else:
            tempDic[key].append(line.replace("\t","").replace(" ","").strip().replace(":",""))
    f.close()

    a = 0
    d = {}

    #Visualize
    for i, keys in tempDic.iteritems():
        d['rec' + str(a)] = {'System': tempDic[i][0], 'Name': tempDic[i][2], 'Version': tempDic[i][1], 'IP': i}
        a += 1
    print d

    app = createTable()
    app.master.title('Visual')
    app.mainloop()

最佳答案

我明白了:Table 有一个预定义的模型, import Dict() 函数仅在该模型上扩展。要修复它,只需创建一个新模型即可。

关于Python Tkinter Table适合窗口太多行/列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43204609/

相关文章:

python - 使用 Python 在 Windows 上传递管理权限

python - 如何将两个字符串转换为 Python 中的函数名称

python - 如何更改或删除 Tkinter 的 OptionMenu 小部件的子菜单边框?

python-3.x - 如何测试使用用户界面的功能

python - tkinter - 形状不统一,动画看起来很糟糕

python - 单击按钮后交换标签

python - Elasticsearch DSL。在聚合内部构建边界框过滤器

python - 有人可以解释这里的逻辑或我在问什么吗? ( bool 运算)

python - 如何在 python 中使用命令行显示特定 uid 或用户名的进程?

python - 我遇到错误 'Attempt to use a closed connection.'