Python 在tkinter中更改treeview中的单个列宽
这里我有一个代码,我想对不同的列有不同的列宽,有办法做到这一点吗?这里的宽度是120,但我不希望所有的列都有120个宽度,因为一个特定的列,我希望其他列有不同的wdith?有什么建议吗?提前感谢: 代码:Python 在tkinter中更改treeview中的单个列宽,python,tkinter,Python,Tkinter,这里我有一个代码,我想对不同的列有不同的列宽,有办法做到这一点吗?这里的宽度是120,但我不希望所有的列都有120个宽度,因为一个特定的列,我希望其他列有不同的wdith?有什么建议吗?提前感谢: 代码: def all_logs(): log = Toplevel(root) log.transient(root) log.title('View all Visitors') # setup treeview columns = ('ID', 'S_I
def all_logs():
log = Toplevel(root)
log.transient(root)
log.title('View all Visitors')
# setup treeview
columns = ('ID', 'S_ID', 'S_NAME', 'B_NAME', 'Date_Taken', 'Due_Date','Date_Returned', 'Status')
tree = ttk.Treeview(log, height=20, columns=columns, show='headings')
tree.grid(row=0, column=0, sticky='news')
# setup columns attributes
for col in columns:
tree.heading(col, text=col)
tree.column(col, width=120, anchor=tk.CENTER)
# fetch data
con = mysql.connect(host='localhost', user='root', password='monkey123', database='BOOK')
c = con.cursor()
c.execute('SELECT * FROM library')
# populate data to treeview
for rec in c:
tree.insert('', 'end', value=rec)
# scrollbar
sb = tk.Scrollbar(log, orient=tk.VERTICAL, command=tree.yview)
sb.grid(row=0, column=1, sticky='ns')
tree.config(yscrollcommand=sb.set)
a = tree.item(tree.focus())['values']
btn = tk.Button(log, text='Close', command=log.destroy, width=20, bd=2, fg='red')
btn.grid(row=1, column=0, columnspan=2)
con.close()
您可以将所需的宽度与列名放在一起:
columns = (
('ID', 50),
('S_ID', 50),
('S_NAME', 120),
('B_NAME', 120),
('Date_Taken', 100),
('Due_Date', 100),
('Date_Returned', 100),
('Status', 50),
)
然后,您需要在创建treeview时提取列名:
tree = ttk.Treeview(log, height=20, columns=[x[0] for x in columns], show='headings')
并配置列标题和宽度:
for col, width in columns:
tree.heading(col, text=col)
tree.column(col, width=width, anchor=tk.CENTER)
您可以使用列='ID',100。。。相反然后更新for循环以适应变化。你是说我必须编辑掉这个部分吗?对于列中的列:tree.headingcol,text=col tree.columncol,width=120,anchor=tk.CENTER?还是别的什么?你能给我一个密码吗?