TypeError:finance_tracker.create_widgets.<locals>.<lambda>()缺少1个必需的位置参数:'col'

问题描述 投票:0回答:1

我在以下代码中遇到错误

finance_tracker 类: def init(自身,根): self.root = 根 self.root.geometry("1000x550") self.root.ressized(False, False) self.root.title('个人理财追踪器') self.root.iconbitmap(r'C:\Users\USER\Desktop\SD1_Courseworks\COURSEWORK_03racker_icon.ico') self.FILE_PATH = '交易.json' self.transactions = self.load_transaction() self.create_widgets() self.max_id = 最大( transaction['id'] 为 self.transactions.values() 中的类别 为 中的交易 类别) if self.transactions else 0 self.search_transactions()

def create_widgets(self):

    # create label
    display_title = tk.Label(self.root, text="Manage Your Transactions Here!",
                             anchor="center", font=TITLE_FONT_STYLE, bg=BLUE_GRAY)
    display_title.pack()

    # Add Some style
    style = ttk.Style()

    # pick a theme
    style.theme_use('clam')  # themes - defaults, alt, clam, vista

    # configure our treeview colors
    style.configure('Treeview',
                    background='White',
                    foreground='Black',
                    rowheight=25,
                    fieldbackground=ASH_GRAY)
    # change selected color
    style.map('Treeview',
              background=[('selected', BUTTERSCOTCH_ORANGE)])

    # create treeview frame
    tree_frame = tk.Frame(self.root)
    tree_frame.configure(bg=BLUE_GRAY)
    tree_frame.pack(fill=tk.BOTH, expand=True, pady=10)

    # create treeview scroll bar
    tree_scroll = tk.Scrollbar(tree_frame)
    tree_scroll.configure(bg=BLUE_GRAY, relief=tk.FLAT)
    tree_scroll.pack(side=tk.RIGHT, fill=tk.Y)

    # create the tree view
    self.my_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set, selectmode=tk.EXTENDED)
    self.my_tree.pack(fill=tk.BOTH, padx=10)

    # defining columns
    self.my_tree['columns'] = (
        'Transaction Type', 'Transaction Category', 'Transaction Amount', 'Transaction Date', 'Transaction ID')

    # configure scroll bars
    tree_scroll.configure(command=self.my_tree.yview)

    # format columns
    self.my_tree.column('#0', width=0, stretch=NO)
    for col in self.my_tree['columns']:
        self.my_tree.column(col, anchor=tk.CENTER, width=120)

    # create headings
    self.my_tree.heading('#0', text='', anchor=tk.W)
    for col in self.my_tree['columns']:
        self.my_tree.heading(col, text=col, anchor=tk.CENTER,
                             command=lambda col: self.sort_by_column(self.my_tree, col, False))

    # create transaction type based striped row tags
    self.my_tree.tag_configure('income_row', background=ROBIN_EGG_BLUE)
    self.my_tree.tag_configure('expense_row', background=VERDIGRIS_BLUE)

    # adding data to the screen
    self.count = 0
    for item in self.transactions.keys():
        for record in self.transactions.get(item):
            if record['type'] == 'Income':
                self.my_tree.insert(parent='', index='end', iid=self.count, text='',
                                    values=(
                                    record['type'], item, f'{record['amount']:,.2f}', record['date'], record['id']),
                                    tags=('income_row',))
            elif record['type'] == 'Expense':
                self.my_tree.insert(parent='', index='end', iid=self.count, text='',
                                    values=(
                                    record['type'], item, f'{record['amount']:,.2f}', record['date'], record['id']),
                                    tags=('expense_row',))
            self.count += 1  # increment counter
def sort_by_column(self, tree, col, reverse):
    for row in tree.get_children():
        data = (tree.set(row, col), row)
        data.sort(reverse=reverse)

        for index in range(len(data)):
            val, row = data[index]
            tree.move(row, '', index)

        tree.heading(col, command=lambda c=col: self.sort_by_column(tree, c, not reverse))

我尝试通过更改 c=col 来更改代码。但没有成功

python python-3.x tkinter lambda typeerror
1个回答
0
投票

正如标题中的错误所述,

command
Treeview.heading()
选项的回调不需要任何参数,因此以下代码中的
lambda
中的
create_widgets()

def create_widgets(self):
    ...
    for col in self.my_tree['columns']:
        self.my_tree.heading(col, text=col, anchor=tk.CENTER,
                             command=lambda col: self.sort_by_column(self.my_tree, col, False))
    ...

应改为:

def create_widgets(self):
    ...
    for col in self.my_tree['columns']:
        self.my_tree.heading(col, text=col, anchor=tk.CENTER,
                             # set default value to col argument
                             command=lambda col=col: self.sort_by_column(self.my_tree, col, False))
    ...
© www.soinside.com 2019 - 2024. All rights reserved.