Search code examples
pythontkinterpandastable

Set alignment of columns in pandastable


I am trying to set different alignment types for different columns in pandastable.

Meanwhile I have found a way to set the alignment for the complete table using the global configuration as shown here (in this example, default alignment "w" can overwritten by "e" by setting the "align" option):

from tkinter import *
from pandastable import Table, TableModel, config

class TestApp(Frame):
    """Basic test frame for the table"""
    def __init__(self, parent=None):
        self.parent = parent
        Frame.__init__(self)
        self.main = self.master
        self.main.geometry('600x400+200+100')
        self.main.title('Table app')
        f = Frame(self.main)
        f.pack(fill=BOTH,expand=1)
        df = TableModel.getSampleData()
        self.table = pt = Table(f, dataframe=df,
                                    showtoolbar=True, showstatusbar=True)
        options = config.load_options()
        options = {'align':'e'}  # set alignment of complete table from "w" to "e"
        config.apply_options(options, pt)        
        pt.show()
        return

app = TestApp()
#launch the app
app.mainloop()

However I have no hint of how to solve this for individual columns.


Solution

  • You can use pt.columnformats['alignment'][colname] to set the alignment of an individual column with name colname.

    For example, to change the alignment for the label column (one of the columns in the data model returned by .getSampleData():

    pt.columnformats['alignment']['label'] = 'w'