【发布时间】:2015-05-30 20:00:11
【问题描述】:
我正在尝试编写一个 python 类来以表格格式显示数据。我确信已经有课程可以做同样的事情,但是,我正在使用这个练习作为自学 Python 和 tkinter 的一种方式。在大多数情况下,我让课程按照我想要的方式工作,但是我无法让标题和数据单元格填充它们的整个单元格,同时左对齐。这是我的班级目前为表格生成的内容:
我继续将单元格上的粘性更改为 (W,E) 而不仅仅是 W,以显示我希望表格的外观,除了每个单元格左对齐。以下是我的目标:
根据我所做的研究,我似乎需要使用grid_columnconfigure 和grid_rowconfigure 的weight 属性,但是我尝试使用它们的每一种方式我都无法使用它们,让它工作。
这是我的课程的代码(我使用的是 Python 3.4):
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()
这是一个引用 TableData 类的测试文件:
from tkinter import *
from tkinter import ttk
from 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()
提前感谢您提供任何见解。请让我知道是否有任何其他有用的信息。
【问题讨论】:
-
我要补充的一点是这个类将用于动态创建表。一旦使用,它不会像我在测试文件中那样将硬编码的数据集发送到类,而是来自查询的结果集。因此,我无法硬编码列的宽度或重量。
-
我在运行您的代码时收到此错误:
UnboundLocalError: local variable 'rowCount' referenced before assignment。您可能需要先解决此问题,然后再更新您的问题。无论如何,拉伸列和行的解决方案实际上是将列和/或行的weight属性设置为1。我会帮忙,但你需要先给我们一个可运行的程序...... -
@Xenomorph,非常感谢您花时间看这个。我添加了 rowCount 的声明,程序现在运行成功。让我知道您是否需要我的其他任何东西。非常感谢。
-
FWIW,
weight选项仅影响为行或列提供多少额外空间。它不会以任何方式影响小部件在其单元格中的对齐方式。 -
@BryanOakley,你知道我为了让单元格填满整个列而不使其居中对齐而缺少什么吗?
标签: python python-3.x tkinter grid