<bdo id='n0IGB'></bdo><ul id='n0IGB'></ul>

<legend id='n0IGB'><style id='n0IGB'><dir id='n0IGB'><q id='n0IGB'></q></dir></style></legend>
  • <tfoot id='n0IGB'></tfoot>
  • <i id='n0IGB'><tr id='n0IGB'><dt id='n0IGB'><q id='n0IGB'><span id='n0IGB'><b id='n0IGB'><form id='n0IGB'><ins id='n0IGB'></ins><ul id='n0IGB'></ul><sub id='n0IGB'></sub></form><legend id='n0IGB'></legend><bdo id='n0IGB'><pre id='n0IGB'><center id='n0IGB'></center></pre></bdo></b><th id='n0IGB'></th></span></q></dt></tr></i><div id='n0IGB'><tfoot id='n0IGB'></tfoot><dl id='n0IGB'><fieldset id='n0IGB'></fieldset></dl></div>

      <small id='n0IGB'></small><noframes id='n0IGB'>

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

        时间:2023-07-23
            <bdo id='V7NjB'></bdo><ul id='V7NjB'></ul>

            <small id='V7NjB'></small><noframes id='V7NjB'>

                1. <i id='V7NjB'><tr id='V7NjB'><dt id='V7NjB'><q id='V7NjB'><span id='V7NjB'><b id='V7NjB'><form id='V7NjB'><ins id='V7NjB'></ins><ul id='V7NjB'></ul><sub id='V7NjB'></sub></form><legend id='V7NjB'></legend><bdo id='V7NjB'><pre id='V7NjB'><center id='V7NjB'></center></pre></bdo></b><th id='V7NjB'></th></span></q></dt></tr></i><div id='V7NjB'><tfoot id='V7NjB'></tfoot><dl id='V7NjB'><fieldset id='V7NjB'></fieldset></dl></div>

                  <tfoot id='V7NjB'></tfoot>
                    <tbody id='V7NjB'></tbody>

                2. <legend id='V7NjB'><style id='V7NjB'><dir id='V7NjB'><q id='V7NjB'></q></dir></style></legend>
                  本文介绍了如何在填充整个单元格时左对齐 Python tkinter 网格列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个 python 类以表格格式显示数据.我确信已经有一些课程可以做同样的事情,但是,我正在使用这个练习作为自学 Python 和 tkinter 的一种方式.在大多数情况下,我让课程按我想要的方式工作,但是我无法让标题和数据单元格填充它们的整个单元格,同时左对齐.这是我的班级目前为表格生成的内容:

                  I'm trying to write a python class to display data in a tabular format. I'm sure there are classes out there already to do the same thing, however, I'm using this exercise as a way to teach myself Python and tkinter. For the most part, I have the class working the way I want it to, however I cannot get the header and data cells to fill their entire cell, while being aligned left. Here is what my class currently generates for a table:

                  我继续将单元格上的粘性更改为 (W,E) 而不仅仅是 W,以显示我希望表格的外观,除了每个单元格左对齐.以下是我的目标:

                  I went ahead and changed the sticky on the cells to be (W,E) rather than just W, in order to show how I want the table to look, except each cell left justified. Below is what I'm shooting for:

                  根据我所做的研究,我似乎需要使用 grid_columnconfiguregrid_rowconfigureweight 属性,但是我尝试过的每一种方式都无法使用它们.

                  Based on the research I've done, it would seem I need to be using the weight attribute of grid_columnconfigure and grid_rowconfigure, however every way I have tried using them I cannot, get it to work.

                  这是我的课程的代码(我使用的是 Python 3.4):

                  Here is the code for my class (I am using 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 类的测试文件:

                  Here is a test file referencing the TableData class:

                  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()
                  

                  提前感谢您提供任何见解.请让我知道是否有任何其他有用的信息.

                  Thanks in advance for any insight. Please, let me know if there is any other info that would be helpful.

                  推荐答案

                  如果您希望标签中的文本左对齐,请使用锚选项.它需要一个代表罗盘上一个点的字符串(例如:w" = west",表示文本锚定在左侧):

                  If you want the text in a label to be left-aligned, use the anchor option. It takes a string representing a point on a compass (eg: "w" = "west", meaning the text is anchored to the left):

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

                  这篇关于如何在填充整个单元格时左对齐 Python tkinter 网格列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 tkinter 使用网格功能显示不同的图像? 下一篇:Python Tkinter 网格复选框

                  相关文章

                    <legend id='48fJN'><style id='48fJN'><dir id='48fJN'><q id='48fJN'></q></dir></style></legend>
                      <bdo id='48fJN'></bdo><ul id='48fJN'></ul>

                      <tfoot id='48fJN'></tfoot>

                    1. <i id='48fJN'><tr id='48fJN'><dt id='48fJN'><q id='48fJN'><span id='48fJN'><b id='48fJN'><form id='48fJN'><ins id='48fJN'></ins><ul id='48fJN'></ul><sub id='48fJN'></sub></form><legend id='48fJN'></legend><bdo id='48fJN'><pre id='48fJN'><center id='48fJN'></center></pre></bdo></b><th id='48fJN'></th></span></q></dt></tr></i><div id='48fJN'><tfoot id='48fJN'></tfoot><dl id='48fJN'><fieldset id='48fJN'></fieldset></dl></div>

                      <small id='48fJN'></small><noframes id='48fJN'>