【问题标题】:Python tkinter Treeview more columns than specifiedPython tkinter Treeview比指定的列更多
【发布时间】:2021-09-25 02:19:30
【问题描述】:

我有一个 ttk Treeview 的以下代码:

listbox = ttk.Treeview(
    tab_player,
    columns=('Player', 'Rating', 'Price'),
    selectmode="extended",
)

listbox.heading('#0', text='Player', anchor=tk.CENTER)
listbox.heading('#1', text='Rating', anchor=tk.CENTER)
listbox.heading('#2', text='Price', anchor=tk.CENTER)
listbox.column('#0', stretch=tk.YES, minwidth=50, width=80)
listbox.column('#1', stretch=tk.YES, minwidth=50, width=20)
listbox.column('#2', stretch=tk.YES, minwidth=50, width=30)

listbox.grid(row=5, column=5, rowspan=7, sticky=W)

我的插入函数如下:

def insertitem():
        GUI.listbox.insert('', 'end', values = (GUI.listbox_content.get(), 
                                                GUI.listboxr_content.get(), 
                                                GUI.listbox_content_price.get()))

当我启动我的应用程序时,我有一个额外的列,插入的数据不是我想要的(数据在错误的列中)。

这是为什么呢?

【问题讨论】:

    标签: python tkinter widget ttk


    【解决方案1】:

    "#0" 始终引用树列,而不是数据列。所以改用"#1", "#2", "#3" 并设置show="headings" 隐藏树列

    listbox = ttk.Treeview(
        tab_player,
        columns=('Player', 'Rating', 'Price'),
        selectmode="extended",
        show="headings"  # hide the tree column
    )
    
    listbox.heading('#1', text='Player', anchor=tk.CENTER)
    listbox.heading('#2', text='Rating', anchor=tk.CENTER)
    listbox.heading('#3', text='Price', anchor=tk.CENTER)
    
    listbox.column('#1', stretch=tk.YES, minwidth=50, width=80)
    listbox.column('#2', stretch=tk.YES, minwidth=50, width=60)
    listbox.column('#3', stretch=tk.YES, minwidth=50, width=60)
    

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2018-03-19
      • 2018-02-14
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      相关资源
      最近更新 更多