_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()  # 父窗口進入事件循環,可以理解爲保持窗口運行,否則界面不展示

 

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