python tkinter 表格_求教关于tkinter treeview模仿表格控件的问题:更新表格的时候控件宽度无法控制...

所以只有用各种办法来模拟。

但是在用的过程中,我用同样的数据来更新表格,窗口会被撑宽,按道理width是由各个columns宽度加起来才对,但结果却很奇怪。。已经试过各种方法来限制宽度了,但结果都是一样的被撑宽。。。

环境:win8.1 python3.3

图1,原图,我希望点了update之后它就能更新数据,而宽度不会发生变化。

145120_eRSS_71614.png

图2,结果就成这个样子了。

145141_Z0m7_71614.png

import tkinter as tk

import tkinter.font as tkFont

import tkinter.ttk as ttk

class McListBox():

"""use a ttk.TreeView as a multicolumn ListBox"""

def __init__(self,table_header = None,table_list = None):

self.table_header = table_header

self.table_list = table_list

self.tree = None

self._setup_widgets()

self._build_tree()

def _setup_widgets(self):

self.container = ttk.Frame()

self.container.pack(fill='both', expand=False)

# create a treeview with dual scrollbars

self.tree = ttk.Treeview(columns=self.table_header, show="headings")

self.vsb = ttk.Scrollbar(orient="vertical",

command=self.tree.yview)

self.hsb = ttk.Scrollbar(orient="horizontal",

command=self.tree.xview)

self.tree.configure(yscrollcommand=self.vsb.set,

xscrollcommand=self.hsb.set)

self.tree.grid(column=0, row=0, sticky='nsew', in_=self.container)

self.vsb.grid(column=1, row=0, sticky='ns', in_=self.container)

self.hsb.grid(column=0, row=1, sticky='ew', in_=self.container)

self.container.grid_columnconfigure(0, weight=1)

self.container.grid_rowconfigure(0, weight=1)

def _build_tree(self):

for col in self.table_header:

self.tree.heading(col, text=col.title(),

command=lambda c=col: sortby(self.tree, c, 0))

# adjust the column's width to the header string

self.tree.column(col,

width=tkFont.Font().measure(col.title()))

for item in self.table_list:

self.tree.insert('', 'end', values=item)

# adjust column's width if necessary to fit each value

for ix, val in enumerate(item):

col_w = tkFont.Font().measure(val)

if self.tree.column(self.table_header[ix],width=None)

self.tree.column(self.table_header[ix], width=col_w)

#print(self.container.width)

def _rebuild_tree(self):

self.tree.configure(columns=self.table_header)

self._build_tree()

def _set_header(self,header = None):

self.table_header = header

def _set_list(self,list = None):

self.table_list = list

def sortby(tree, col, descending):

"""sort tree contents when a column header is clicked on"""

# grab values to sort

data = [(tree.set(child, col), child) \

for child in tree.get_children('')]

# if the data to be sorted is numeric change to float

#data = change_numeric(data)

# now sort the data in place

data.sort(reverse=descending)

for ix, item in enumerate(data):

tree.move(item[1], '', ix)

# switch the heading so it will sort in the opposite direction

tree.heading(col, command=lambda col=col: sortby(tree, col, \

int(not descending)))

if __name__ == "__main__" :

# the test data ...

car_header = ['car', 'repair']

car_list = [

('Hyundai', 'brakes') ,

('Honda', 'light') ,

('Lexus', 'battery') ,

('Benz', 'wiper') ,

('Ford', 'tire') ,

('Chevy', 'air') ,

('Chrysler', 'piston') ,

('Toyota', 'brake pedal') ,

('BMW', 'seat')

]

root = tk.Tk()

root.wm_title("multicolumn ListBox")

mc_listbox = McListBox(table_header=car_header,table_list=car_list)

def update_table():

for i in map(mc_listbox.tree.delete,mc_listbox.tree.get_children('')):

pass

mc_listbox._rebuild_tree()

print(mc_listbox.tree.winfo_width())

tk.Button(root,text="update",command=update_table).pack()

root.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值