Search code examples
python-3.xtkinterttkbootstrap

why does ttkbootstrap Tableview widget automatically get wider on rebuild and ignore autofit_columns()?


I love the ttkbootstrap project, but I've been pulling my hair out trying to figure out why this is happening:

  1. I create a table and display it in a window, I choose autofit=True so it fits nicely
  2. The data changes, and I need to update the window. I do this using the build_table_data method
  3. After rebuilding the table I try to use the autofit_columns method to make the table nice and concise again, but nothing happens and the table grows in width, even though the columns are nice and small. What am I doing wrong??

Here's my Code:

import tkinter as tk
import ttkbootstrap as ttkb
from ttkbootstrap.tableview import Tableview
from ttkbootstrap.constants import *

class MainAppWindow(ttkb.Window):
    def __init__(self,root):
        self.window = root
        self.window.geometry('900x600')

        #initialize table
        self.coldata=[{"text":"Spam","stretch":True},
                 {"text":"Beans","stretch":True},
                 {"text":"Eggs","stretch":True},
                 {"text":"Spam","stretch":True},
                 {"text":"Eggs","stretch":True},]
        self.rowdata=[]
        self.table=ttkb.tableview.Tableview(master = self.window,
                                            coldata=self.coldata,
                                            rowdata=self.rowdata,
                                            paginated=True,
                                            searchable=True,
                                            autofit=True)

        #at this point, the table is nice and neat, and doesn't take up alot of space
        #now add a button that will change
        self.button=ttkb.Button(text="change data",command=self.change_data)

        #pack table and button
        self.table.pack()
        self.button.pack()


        self.window.mainloop()

    def change_data(self):
        self.rowdata = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],]
        self.table.build_table_data(coldata=self.coldata,rowdata=self.rowdata)
        self.table.autofit_columns()


root=ttkb.Window()
MainAppWindow(root)

Solution

  • Hey ☺️ I finally got the solution ! Just call the reset_table() function after build_table_data() instead of autofit_columns() as shown :

    import tkinter as tk
    import ttkbootstrap as ttkb
    from ttkbootstrap.tableview import Tableview
    from ttkbootstrap.constants import *
    
    class MainAppWindow(ttkb.Window):
    def __init__(self,root):
        self.window = root
        self.window.geometry('900x600')
    
        #initialize table
        self.coldata=[{"text":"Spam","stretch":True},
                 {"text":"Beans","stretch":True},
                 {"text":"Eggs","stretch":True},
                 {"text":"Spam","stretch":True},
                 {"text":"Eggs","stretch":True},]
        self.rowdata=[]
        self.table=ttkb.tableview.Tableview(master = self.window,
                                            coldata=self.coldata,
                                            rowdata=self.rowdata,
                                            paginated=True,
                                            searchable=True,
                                            autofit=True)
    
        #at this point, the table is nice and neat, and doesn't take up alot of space
        #now add a button that will change
        self.button=ttkb.Button(text="change data",command=self.change_data)
    
        #pack table and button
        self.table.pack()
        self.button.pack()
    
    
        self.window.mainloop()
    
    def change_data(self):
        self.rowdata = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],]
        self.table.build_table_data(coldata=self.coldata,rowdata=self.rowdata)
        self.table.reset_table()
    
    
    root=ttkb.Window()
    MainAppWindow(root)
    

    Happy coding 😄