请教要如何解决ttk的Treeview列宽和边框线闭合

1
import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.title(‘text’)
root.geometry(‘800x400’)
frame = tk.LabelFrame(root, text=’分析区’, font=(‘宋体’, 10), fg=’red’, labelanchor=’n’, bd=1, height=140, width=600,
relief=’groove’)
frame.pack(side=’left’, pady=2, padx=2)
frame.pack_propagate(0)
fram1 = tk.Frame(frame, width=20, height=20)
fram1.pack(side=’left’)

mark = [str(i) for i in range(1, 21)]
table = ttk.Treeview(frame, columns=mark, height=5, style=’Treeview’, show=’headings’)

for j in range(len(mark)):
table.heading(column=mark[j], text=mark[j], anchor=’center’)
table.column(j, minwidth=0, width=28, anchor=’center’, stretch=tk.NO)
table.pack()

root.mainloop()

Jason990420
最佳答案

Column identifiers take any of the following forms:

  • A symbolic name from the list of -columns.
  • An integer n, specifying the nth data column.
  • A string of the form #n, where n is an integer, specifying the nth display column.

column #0 always refers to the tree column.

The cid for method Column should be the mark[j], j+1 or f'#{j+1}', not j.

It means that you didn’t set the width for column '20', and the default width is 200 pixels, so the treeview will exceed the visible range of FRAME.

for j in range(len(mark)):
    table.heading(column=mark[j], text=mark[j], anchor='center')
    table.column(mark[j], minwidth=0, width=28, anchor='center', stretch=tk.NO)


Of course, you can remove the option `width=600` from the `frame`, also the statement `frame.pack_propagate(0)`.

4个月前 评论
LiTtianS (楼主) 3个月前
讨论数量: 2
Jason990420

Column identifiers take any of the following forms:

  • A symbolic name from the list of -columns.
  • An integer n, specifying the nth data column.
  • A string of the form #n, where n is an integer, specifying the nth display column.

column #0 always refers to the tree column.

The cid for method Column should be the mark[j], j+1 or f'#{j+1}', not j.

It means that you didn’t set the width for column '20', and the default width is 200 pixels, so the treeview will exceed the visible range of FRAME.

for j in range(len(mark)):
    table.heading(column=mark[j], text=mark[j], anchor='center')
    table.column(mark[j], minwidth=0, width=28, anchor='center', stretch=tk.NO)


Of course, you can remove the option `width=600` from the `frame`, also the statement `frame.pack_propagate(0)`.

4个月前 评论
LiTtianS (楼主) 3个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!