如何在填充整个单元格时左对齐Python tkinter网格列

时间:2015-05-30 20:00:12

标签: python python-3.x tkinter grid

我正在尝试编写一个python类来以表格格式显示数据。我确信那里已经有类做同样的事情,但是,我正在使用这个练习来教自己Python和tkinter。在大多数情况下,我让这个类按照我想要的方式工作,但是当我向左对齐时,我无法获得标题和数据单元来填充整个单元格。这是我的班级当前为表格生成的内容:

How the table currently looks

我继续将单元格上的粘性变为(W,E)而不是W,以便显示我希望表格看起来如何,除了每个单元格左对齐。以下是我正在拍摄的内容:

How I want the table to look

根据我所做的研究,似乎我需要使用weightgrid_columnconfigure的{​​{1}}属性,但是我尝试过使用它们的每一种方法都不能让它发挥作用。

这是我班级的代码(我使用的是Python 3.4):

grid_rowconfigure

这是一个引用TableData类的测试文件:

from tkinter import *
from tkinter import ttk
from 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
        for i 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)
        for 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
        for row in self.data:
            if rowCount % 2 == 0:
                rowColor = self.dataRowColor2
            else:
                 rowColor = self.dataRowColor1
            colCount = 0
            for 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()

提前感谢任何见解。如果有任何其他信息有用,请告诉我。

4 个答案:

答案 0 :(得分:10)

对于任何几何网格,添加选项sticky="W",例如

self.tableDataFrame.grid(sticky="W", row=0, column=0)

答案 1 :(得分:5)

如果希望标签中的文本左对齐,请使用锚点选项。它需要一个表示指南针上一个点的字符串(例如:“w”=“west”,意味着文本固定在左侧):

for col in row:
    Label(..., anchor="w").grid(...)

答案 2 :(得分:0)

您没有定义标签的宽度,因此tkinter将Label的宽度设置为等于文本的宽度。这就是为什么您的标签不占据单元格的整个宽度的原因。为了使它们占据所有可用宽度,请使用stickey = E + W

    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+E, ipady=2, ipadx=5)

答案 3 :(得分:-2)

尝试在griding时设置columnspan属性:

self.tableDataFrame.grid(row=0, column=0, columnspan=2)

2可能不是正确的尺寸,请尝试直到你想要它。