_tkinter.TclError: Invalid column index g

最近在学Python3的tkinter模块使用tkinter的Treeview做表格展示信息,出现如下错误_tkinter.TclError: Invalid column index g

观察代码后发现是在columnheading中加了两列,并没有在定义的时候添加,导致报错,加上之后代码正常运行

下面附上完整代码,列表展示并带有滚动条

from tkinter import *
from tkinter.ttk import Treeview
from tkinter.ttk import Scrollbar

# 顶层窗口
top = Tk()  # 创建顶层窗口
top.title("工具")
top.geometry('650x405')  # 290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
# 创建一个表格
table = Treeview(top, show='headings', column=('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'), height=16)
# 创建滚动条
vbar = Scrollbar(top, orient="vertical", command=table.yview)
# 定义树形结构与滚动条
table.configure(yscroll=vbar.set)
table.grid(row=2, columnspan=7)
vbar.grid(row=2, column=7, sticky='NS')  # N/S/E/W,分别代表上对齐/下对齐/左对齐/右对齐
table.column('a', width=60, anchor="center")
table.column('b', width=60, anchor="center")
table.column('c', width=60, anchor="center")
table.column('d', width=60, anchor="center")
table.column('e', width=60, anchor="center")
table.column('f', width=60, anchor="center")
table.column('g', width=60, anchor="center")
table.column('h', width=60, anchor="center")
table.column('i', width=60, anchor="center")
table.column('j', width=60, anchor="center")
table.heading('a', text='xx')
table.heading('b', text='xxx')
table.heading('c', text='xxxx')
table.heading('d', text='xxx')
table.heading('e', text='xx')
table.heading('f', text='xx')
table.heading('g', text='xxx')
table.heading('h', text='xxx')
table.heading('i', text='xxx')
table.heading('j', text='x')


top.mainloop()  # 父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章