I am trying to use python tkinter to build a interface to show database table information with Treeview. The heading is the table schema, and insert data row to the treeview table. Since the number of column in different tables may not be the same, I want to auto adjust the width of column so that the total width of the treeview table is fixed.
I wrote the python3 code in Pycharm 2018.3-community, and used a function(show table) to insert the data rows and used "treeview.column(col, width = int( total_width / len(cols)), minwidth=0, anchor='center',stretch=0 )" to set the column width. the function is called when showing the page(I put comment for the line when its called). When the code was run the first time, the column width was adjusted by the width normally and the total table width is well fit and correct, as shown below.
However, if I click a button to call the same function to insert the same data row, then the treeview table width will change. and I found it change with the number of column, as shown below. I do not know why it happened like this. Please advise. Thanks a lot ! my code is attached at the bottom.
class ListPage(tk.Tk):
table_list = sample.scan_result.table_list
table_index = 0
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='#C6E2FF')
self.tbTitlelabel = tk.Button(self, text="Table", font=("Verdana", 18, "bold"), bg='#C6E2FF', fg='darkblue', command=lambda: controller.show_frame(start_page.StartPage))
self.tbTitlelabel.grid(row=1, column=0, columnspan=1, padx=(150, 0), sticky='w')
self.nextLabel = tk.Button(self, text="Next", font=("Verdana", 8, "bold"), bg='#C6E2FF', fg='darkblue',command=lambda:self.nextTable())
self.nextLabel.grid(row=1, column=0, columnspan=1, padx=(280, 0), sticky='w')
**# if call the function by the button, the table width change.**
self.preLabel = tk.Button(self, text="Previous", font=("Verdana", 18, "bold"), bg='#C6E2FF', fg='darkblue', command=lambda:self.showTable() )
self.preLabel.grid(row=1, column=0, columnspan=1, padx=(380, 0), sticky='w')
style = ttk.Style()
style.configure("Treeview.Heading", font=("Verdana", 15))
self.tree = ttk.Treeview(self, height=10, show = 'headings', selectmode='browse')
self.tree.grid(row=2, column=0, columnspan=1, padx=(120, 0), sticky='w')
ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
self.tree['yscroll'] = ysb.set
ysb.grid(row=2, column=0, sticky='ens')
**# if call the function during the page intial, the table width is good.**
self.showTable()
def nextTable(self):
if self.table_index < len(self.table_list)-1:
self.table_index += 1
self.showTable()
def previousTable(self):
if self.table_index > 0:
self.table_index -= 1
self.showTable()
def showTable(self):
print('run show table')
col_start_pos = 0
for i in self.tree.get_children():
self.tree.delete(i)
if len(self.table_list) > self.table_index:
print( self.table_index, self.table_list)
table_item = self.table_list[self.table_index]
print(self.table_list[1], self.table_index)
self.tbTitlelabel['text'] = table_item.db_name
col_name = table_item.col_name[col_start_pos:]
self.tree['columns'] = col_name
data_list = table_item.data_list
for col in col_name:
self.tree.heading(col, text=col)
**self.tree.column(col, width= int(600 / len(col_name)), minwidth=0, anchor='center',stretch=0) # auto adjust column width**
x=0
while x < 7:
for list in data_list:
self.tree.insert('', 'end', values = list[col_start_pos:] )
x+=1