【问题标题】:Table of widgets in Python with Tkinter带有 Tkinter 的 Python 中的小部件表
【发布时间】:2013-05-17 15:39:58
【问题描述】:

我想在 python 下使用 Tkinter 将表格放入 GUI 中的标签框架 该表不仅包含静态数据,还包含按钮、输入条目、检查按钮等小部件

例如:

表 1:

[ Nr. | Name | Active |  Action  ]
----------------------------------
[ 1   |  ST  |  [x]   | [Delete] ]
[ 2   |  SO  |  [ ]   | [Delete] ]
[ 3   |  SX  |  [x]   | [Delete] ]

[x] 是一个检查按钮,[Delete] 是一个按钮

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您可以在框架中使用grid 几何管理器来根据需要布置小部件。这是一个简单的例子:

    import Tkinter as tk
    import time
    
    class Example(tk.LabelFrame):
        def __init__(self, *args, **kwargs):
            tk.LabelFrame.__init__(self, *args, **kwargs)
            data = [
                # Nr. Name  Active
                [1,   "ST", True],
                [2,   "SO", False],
                [3,   "SX", True],
                ]
    
            self.grid_columnconfigure(1, weight=1)
            tk.Label(self, text="Nr.", anchor="w").grid(row=0, column=0, sticky="ew")
            tk.Label(self, text="Name", anchor="w").grid(row=0, column=1, sticky="ew")
            tk.Label(self, text="Active", anchor="w").grid(row=0, column=2, sticky="ew")
            tk.Label(self, text="Action", anchor="w").grid(row=0, column=3, sticky="ew")
    
            row = 1
            for (nr, name, active) in data:
                nr_label = tk.Label(self, text=str(nr), anchor="w")
                name_label = tk.Label(self, text=name, anchor="w")
                action_button = tk.Button(self, text="Delete", command=lambda nr=nr: self.delete(nr))
                active_cb = tk.Checkbutton(self, onvalue=True, offvalue=False)
                if active:
                    active_cb.select()
                else:
                    active_cb.deselect()
    
                nr_label.grid(row=row, column=0, sticky="ew")
                name_label.grid(row=row, column=1, sticky="ew")
                active_cb.grid(row=row, column=2, sticky="ew")
                action_button.grid(row=row, column=3, sticky="ew")
    
                row += 1
    
        def delete(self, nr):
            print "deleting...nr=", nr
    
    if __name__ == "__main__":
        root = tk.Tk()
        Example(root, text="Hello").pack(side="top", fill="both", expand=True, padx=10, pady=10)
        root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多