How to left justify Python tkinter grid columns while filling entire cell -
i'm trying write python class display data in tabular format. i'm sure there classes out there same thing, however, i'm using exercise way teach myself python , tkinter. part, have class working way want to, cannot header , data cells fill entire cell, while being aligned left. here class generates table:

i went ahead , changed sticky on cells (w,e) rather w, in order show how want table look, except each cell left justified. below i'm shooting for:

based on research i've done, seem need using weight attribute of grid_columnconfigure , grid_rowconfigure, every way have tried using them cannot, work.
here code class (i using python 3.4):
from tkinter import * tkinter import ttk tkinter import font class tabledata: def __init__(self,parent,attributes,columns,data): self.parent = parent self.tablename = stringvar() self.tablename.set(attributes['tablename']) self.columns = columns self.columncount = 0 self.bordercolor = attributes['bordercolor'] self.titlebg = attributes['titlebg'] self.titlefg = attributes['titlefg'] self.titlefontsize = attributes['titlefontsize'] self.headerbg = attributes['headerbg'] self.headerfg = attributes['headerfg'] self.headerfontsize = attributes['headerfontsize'] self.datarowcolor1 = attributes['datarowcolor1'] self.datarowcolor2 = attributes['datarowcolor2'] self.datarowfontsize = attributes['datarowfontsize'] self.datarowfg = attributes['datarowfg'] self.data = data self.tabledataframe = ttk.frame(self.parent) self.tabledataframe.grid(row=0,column=0) self.initui() def countcolumns(self): cnt = 0 in self.columns: cnt += 1 self.columncount = cnt def buildtabletitle(self): tabletitlefont = font.font(size=self.titlefontsize) label(self.tabledataframe,textvariable=self.tablename,bg=self.titlebg,fg=self.titlefg,font=tabletitlefont, highlightbackground=self.bordercolor,highlightthickness=2).grid(row=0,columnspan=self.columncount,sticky=(w,e), ipady=3) def buildheaderrow(self): colcount = 0 tableheaderfont = font.font(size=self.headerfontsize) col in self.columns: label(self.tabledataframe,text=col,font=tableheaderfont,bg=self.headerbg,fg=self.headerfg,highlightbackground=self.bordercolor,highlightthickness=1).grid(row=1,column=colcount,sticky=w, ipady=2, ipadx=5) colcount += 1 def builddatarow(self): tabledatafont = font.font(size=self.datarowfontsize) rowcount = 2 row in self.data: if rowcount % 2 == 0: rowcolor = self.datarowcolor2 else: rowcolor = self.datarowcolor1 colcount = 0 col in row: label(self.tabledataframe,text=col,bg=rowcolor,fg=self.datarowfg,font=tabledatafont,highlightbackground=self.bordercolor,highlightthickness=1).grid(row=rowcount,column=colcount,sticky=w,ipady=1, ipadx=5) colcount += 1 rowcount += 1 def initui(self): self.countcolumns() self.buildtabletitle() self.buildheaderrow() self.builddatarow() here test file referencing tabledata class:
from tkinter import * tkinter import ttk tabledata import tabledata import sqlite3 root = tk() root.geometry('1000x400') mainframe = ttk.frame(root).grid(row=0,column=0) attributes = {} attributes['tablename'] = 'title' attributes['bordercolor'] = 'black' attributes['titlebg'] = '#1975d1' attributes['titlefg'] = 'white' attributes['titlefontsize'] = 16 attributes['headerbg'] = 'white' attributes['headerfg'] = 'black' attributes['headerfontsize'] = 12 attributes['datarowcolor1'] = 'white' attributes['datarowcolor2'] = 'grey' attributes['datarowfontsize'] = 10 attributes['datarowfg'] = 'black' columns = ['col 1', 'column 2', 'column 3','column 4'] results = [('1','key','desc','attribute'),('2','key column','description column','attributecolumn')] table = tabledata(mainframe,attributes,columns,results) root.mainloop() thanks in advance insight. please, let me know if there other info helpful.
if want text in label left-aligned, use anchor option. takes string representing point on compass (eg: "w" = "west", meaning text anchored left):
for col in row: label(..., anchor="w").grid(...)
Comments
Post a Comment