如何在Tkinter TkTable

时间:2017-09-06 19:58:25

标签: python tkinter tktable

我在跟踪TkTable文档时遇到了如何将预定义值插入到TkTable for Tkinter GUI中的问题。文档:https://www.eso.org/projects/vlt/sw-dev/tcl8.3.3/tktable2.7/tkTable.html

我研究了这个并且我无法在任何地方找到合适的答案来记录如何将预定义值插入到TkTable中......这是我发现的一个未解答的问题: set value into tktable

有谁知道这是怎么做的?

这是我从下载TkTable后提供的示例派生的示例代码(我可以将1只狗设置到表中,但我不知道如何使用set方法成功迭代列表以在另一个之后插入一个单元格):

import tkinter as tk
import tktable

def table_test():

    #### DEFINES THE GUI ####
    master = tk.Tk()
    master.geometry('800x800+250+200')
    master.title('Dogs')

    #### SETS THE DOG INTO THE TABLE ####
    def callback_dogs(event):
        values = ['Doodle','Pug','Westie','Poodle']
        for item in values:
            return item

    #### DEFINING THE TKTABLE ####
    tb = tktable.Table(master, state='disabled', width=15, titlerows=1, rows=3, cols=2, command=callback_dogs)
    tb.pack(expand=1)

    #### MAINLOOPING ####
    master.attributes('-topmost',True)
    master.after_idle(master.attributes,'-topmost',False)
    tk.mainloop()

table_test()

1 个答案:

答案 0 :(得分:1)

我在重新审视TkTable下载中的示例后,通过定义tktable.ArrayVar(master)变量找出了解决方案。如果有更好的解决方案,请发布。

以下是我的示例,我可以将预先存在的数据值加载到TkTable中:

import tkinter as tk
import tktable

def table_test():
    #### DEFINES THE INTERFACE ####
    master = tk.Tk()
    master.geometry('500x200+250+200')
    master.title('Dogs')
    #### DEFINING THE TABLE ####
    tb = tktable.Table(master,
                       state='disabled',
                       width=50,
                       titlerows=1,
                       rows=5,
                       cols=4,
                       colwidth=20)
    columns = ['Breed','Price','Location','Age']
    #### LIST OF LISTS DEFINING THE ROWS AND VALUES IN THOSE ROWS ####
    values = [['Doodle','1500','Chicago','1'],
              ['Pug','700','Kansas City','2'],
              ['Westie','1000','Lincoln','1'],
              ['Poodle','900','Atlanta','2']]
    #### SETS THE DOGS INTO THE TABLE ####
    #### VVVVVVVVVVVVVVVVVVVVVVVVVVV ####
    #DEFINING THE VAR TO USE AS DATA IN TABLE
    var = tktable.ArrayVar(master)
    row_count=0
    col_count=0
    #SETTING COLUMNS
    for col in columns:
        index = "%i,%i" % (row_count,col_count)
        var[index] = col
        col_count+=1
    row_count=1
    col_count=0
    #SETTING DATA IN ROWS
    for row in values:
        for item in row:
            print(item)
            index = "%i,%i" % (row_count,col_count)
            ## PLACING THE VALUE IN THE INDEX CELL POSITION ##
            var[index] = item
            #### IGNORE THIS IF YOU WANT, JUST SETTING SOME CELL COLOR ####
            try:
                if int(item) > 999:
                    tb.tag_cell('green',index)
            except:
                pass
            ###############################################################
            col_count+=1       
        col_count=0
        row_count+=1
    #### ABOVE CODE SETS THE DOG INTO THE TABLE ####
    ################################################
    #### VARIABLE PARAMETER SET BELOW ON THE 'TB' USES THE DATA DEFINED ABOVE ####
    tb['variable'] = var
    tb.pack()
    #tb.tag_cell('green',index)
    tb.tag_configure('green', background='green')
    #### MAINLOOPING ####
    tk.mainloop()
table_test()

Example TkTable