-1

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
tfv
  • 6,016
  • 4
  • 36
  • 67

1 Answers1

3

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'
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Perfect, thank you! Where did you find this information? I cannot find the search term 'columnformats' in the pandastable documentation at https://pandastable.readthedocs.io/en/latest/pandastable.html . And (sorry for this follow-up, would be a new question) is there a way to also change the individual width of one column? Using 'width' instead of 'alignment' does not work. – tfv Nov 01 '21 at 05:35
  • 2
    @tfv You need to look into the source code of `pandastable`. For individual width, use `pt.columnwidths[...]`, for example `pt.columnwidths['label'] = 100`. – acw1668 Nov 01 '21 at 05:53
  • Very helpful. This should be marked as solved. :) – fishbacp Apr 19 '22 at 16:49