Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

python - Why do treeview widget put an extra column although I only gave it two? I also can't control the width of it

  1. I can't figure out how to control the width of the widget
  2. I can't make it display only 2 columns

    from Tkinter import *
    from ttk import Treeview
    root = Tk()
    tree = Treeview(root, height = 10, columns = 2)
    tree['columns'] = ('one','two')
    tree.column('one', width = 50)
    tree.column('two', width = 50)
    tree.heading('one', text = 'UserName', anchor = 'center')
    tree.heading('two', text = 'ID', anchor = 'centeenter code herer')
    tree.grid(row = 3, column = 0)
    root.mainloop()
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That first column is the tree. You can turn it off by using the show attribute. The value must be a list with zero or more values. The valid values are headings to show the column headings, and tree to show the tree. The default value is ['tree', 'headings'].

Here's how to have the treeview show the column headings but not the tree:

tree = Treeview(root, height=10, columns=2, show=["headings"])

If you want to see the tree, but you want to control its width, you can do that too. The tree column can always be identified with '#0'. You can use the column method to set the width:

tree.column('#0', width=50)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...