【问题标题】:How to align column headers in tkinter table python如何在 tkinter 表 python 中对齐列标题
【发布时间】:2017-09-23 20:38:04
【问题描述】:

我正在尝试组织标题,使它们与列对齐。下面是代码(创建表),但它没有按照我想要的方式工作。:

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)               
        names = ["header1", "header2", "header3", "header4"]
        self.label1 = tk.Label(self, text=("        ".join(names)))
        self.table = SimpleTableInput(self, 3, 4)
        self.submit = tk.Button(self, text="Submit", command=self.on_submit)
        self.label1.pack(side="top")
        self.table.pack(side="top", fill="both", expand=True)
        self.submit.pack(side="bottom")

我很感激任何建议!谢谢

整个脚本在这里:

import tkinter as tk

class SimpleTableInput(tk.Frame):
    def __init__(self, parent, rows, columns):
        tk.Frame.__init__(self, parent)

        self._entry = {}
        self.rows = rows
        self.columns = columns

        # register a command to use for validation
        vcmd = (self.register(self._validate), "%P")
        # create the table of widgets
        for row in range(self.rows):
            for column in range(self.columns):
                index = (row, column)
                e = tk.Entry(self, validate="key", validatecommand=vcmd)
                e.grid(row=row, column=column, stick="nsew")
                self._entry[index] = e
        # adjust column weights so they all expand equally
        for column in range(self.columns):
            self.grid_columnconfigure(column, weight=1)
        # designate a final, empty row to fill up any extra space
        self.grid_rowconfigure(rows, weight=1)

    def get(self):
        '''Return a list of lists, containing the data in the table'''
        result = []
        for row in range(self.rows):
            current_row = []
            for column in range(self.columns):
                index = (row, column)
                current_row.append(self._entry[index].get())
            result.append(current_row)
        return result

    def _validate(self, P):
        '''Perform input validation. 

        Allow only an empty value, or a value that can be converted to a float
        '''
        if P.strip() == "":
            return True

        try:
            f = float(P)
        except ValueError:
            self.bell()
            return False
        return True

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)               
        names = ["header1", "header2", "header3", "header4"]
        self.label1 = tk.Label(self, text=("        ".join(names)))
        self.table = SimpleTableInput(self, 3, 4)
        self.submit = tk.Button(self, text="Submit", command=self.on_submit)
        self.label1.pack(side="top")
        self.table.pack(side="top", fill="both", expand=True)
        self.submit.pack(side="bottom")

    def on_submit(self):
        print(self.table.get())

root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()

【问题讨论】:

  • 很难将标签中的文本与不同小部件的列对齐。你能把SimpleTableInput的代码贴出来吗?也许你有一些选项可以直接在里面显示标题?
  • 我已经发布了整个代码,SimpleTableInput 是其中的一部分。谢谢

标签: python tkinter


【解决方案1】:

我建议的方法是在顶部创建一个框架,并在分隔标签内填充names。它允许对齐列并保持这种对齐,即使窗口已展开:

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        names = ["header1", "header2", "header3", "header4"]
        frame = tk.Frame(self)
        frame.pack(side="top", fill="both", expand=True)
        for i, title in enumerate(names):
            l = tk.Label(frame, text=title)
            l.grid(row=0, column=i)
            frame.grid_columnconfigure(i, weight=1)
        self.table = SimpleTableInput(self, 3, 4)
        self.submit = tk.Button(self, text="Submit", command=self.on_submit)
        self.table.pack(side="top", fill="both", expand=True)
        self.submit.pack(side="bottom")

【讨论】:

  • 您的解决方案正是我所追求的。谢谢!
猜你喜欢
  • 2018-01-08
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 1970-01-01
相关资源
最近更新 更多