Answer a question

I have created a GUI for my application with Tkinter. I am using also a treeview widget. However I am unable to change its column widths and weights. How to do it properly?

Sample:

tree = Treeview(frames[-1],selectmode="extended",columns=("A","B"))
tree.heading("#0", text="C/C++ compiler")
tree.column("#0",minwidth=0,width=100)

tree.heading("A", text="A")   
tree.column("A",minwidth=0,width=200) 

tree.heading("B", text="B")   
tree.column("B",minwidth=0,width=300) 

As far as I understand it should create three columns with widths: 100,200 and 300. However nothing like that happens.

Answers

Treeview.Column does not have weight option, but you can set stretch option to False to prevent column resizing.

from tkinter import *
from tkinter.ttk import *


root = Tk()
tree = Treeview(root, selectmode="extended", columns=("A", "B"))
tree.pack(expand=YES, fill=BOTH)
tree.heading("#0", text="C/C++ compiler")
tree.column("#0", minwidth=0, width=100, stretch=NO)
tree.heading("A", text="A")
tree.column("A", minwidth=0, width=200, stretch=NO) 
tree.heading("B", text="B")
tree.column("B", minwidth=0, width=300)
root.mainloop()
Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐