why is this showing a TypeError: View_Account.go_to_modify_account() missing 1 required positional argument: 'id'

huangapple go评论64阅读模式
英文:

why is this showing a TypeError: View_Account.go_to_modify_account() missing 1 required positional argument: 'id'

问题

更新按钮不起作用的原因是在 self.modify_account 方法中,您尝试使用 id 参数,但没有为该方法提供该参数。这可能导致类型错误。

您可以尝试做以下修改,将 self.modify_account 方法修改为不需要参数的方法,然后在该方法内部获取选定行的 id

def modify_account(self):
    if not self.table.selection():
        messagebox.showinfo('Update Account', 'Please select an account to modify')
    else:
        for selected_row in self.table.selection():
            row = self.table.item(selected_row)
            id = row['values'][0]
            self.go_to_modify_account(id)

通过这种方式,modify_account 方法将从选定行中获取 id,而无需在方法调用时传递参数。这应该解决类型错误问题。如果仍然存在问题,请提供更多详细信息以供进一步帮助。

英文:
class View_Account(tk.Frame):
    def __init__(self,master):
        tk.Frame.__init__(self,master)

        self.parent = master
    
        main_frame = tk.Frame(self, width=750, height=450, bg='white')
        main_frame.pack(fill='both', expand='yes')

        self.Frame1 = tk.Frame(self, width=750, height=100, bg='pink')
        self.Frame1.place(x=0, y=0)

        self.Frame2 = tk.Frame(self, width=750, height=450, bg='#E8E4C9')
        self.Frame2.place(x=0, y=100)

        self.view_acc = tk.Label(self, text='View Account', font=('Algerian', 26), bg='pink')
        self.view_acc.place(x=90, y=10)

        self.modify_acc = tk.Button(self, text='Modify Account', font=('Algerian', 13), command=self.go_to_modify_account)
        self.modify_acc.place(x= 40, y=60)

        self.delete_acc = tk.Button(self, text='Delete Account', font=('Algerian', 13), command=self.delete_account)
        self.delete_acc.place(x= 200, y=60)

        self.back_button = tk.Button(self, text='Back', font=('Algerian', 13), command=self.go_to_admin_dashboard)
        self.back_button.place(x= 5, y=5)

        columns = ('id','fullname','birthdate','address','email','contact_number','card_number','pin','initial_deposit')
        
        self.table = ttk.Treeview(self, columns=columns, show='headings', selectmode='browse')
        self.table.heading('id', text='id')
        self.table.heading('fullname', text='Name')
        self.table.heading('birthdate', text='Birthdate')
        self.table.heading('address', text='Address')
        self.table.heading('email', text='Email')
        self.table.heading('contact_number', text='Contact #')
        self.table.heading('card_number', text='Card Number')
        self.table.heading('pin',text='PIN')
        self.table.heading('initial_deposit',text='Initial Deposit')

        self.table.column('id', width=5)
        self.table.column('fullname', width=70)
        self.table.column('birthdate', width=70)
        self.table.column('address', width=115)
        self.table.column('email', width=145)
        self.table.column('contact_number', width=80)
        self.table.column('card_number', width=115)
        self.table.column('pin', width=50)
        self.table.column('initial_deposit', width=100)

        self.table.place(x=0, y=102)

        self.table.configure(height=16)

        self.update_table()

    def update_table(self):
        self.client_account = self.get_client_account()
        
        self.table.delete(*self.table.get_children())

        for client in self.client_account:
            row = (
                client.id,
                client.fullname,
                client.birthdate,
                client.address,
                client.email,
                client.contact_number,
                client.card_number,
                client.pin,
                client.initial_deposit
            )
            self.table.insert('', tk.END, values=row)
    
    def get_client_account(self):
        db_conn = database_handler.DBHandler()
        self.client_account = db_conn.view_accounts()
        db_conn.close()

        return self.client_account
    
    def delete_account(self):
        if len(self.table.selection()) == 0:
            messagebox.showerror('Error', 'Select Account to delete.')
        else:
            proceed = messagebox.askyesno('Account Deletion', 'Do you wish to continue?')
            if not proceed:
                return
            for selected_row in self.table.selection():
                row = self.table.item(selected_row)
                id = row['values'][0]
                db_conn = database_handler.DBHandler()
                db_conn.delete_accounts(id)
                db_conn.close()

                self.update_table()
    
    def go_to_modify_account(self, id):
        self.parent.change_window('Modify_Account', id=id)
    
    def modify_account(self):
        if not self.table.selection():
            messagebox.showinfo('Update Account', 'Please select an account to modify')
        else:
            for selected_row in self.table.selection():
                row = self.table.item(selected_row)
                id = row['values'][0]
                self.go_to_modify_account(id)


    def go_to_admin_dashboard(self):
        self.parent.change_window('Admin_Dashboard')

    def on_return(self):
        pass

why does the update button not work. it shows type error

i tried adding self to the id but it still does not work

答案1

得分: 2

根据文档,command= 需要一个没有参数的函数。所以你使用 command=self.foo 意味着你将调用 self.foo(),并且没有 id 参数。

为什么你在 go_to_modify_account 中期望一个 id 参数,但在其他地方没有?你是不是想要使用 command=modify_account

英文:

At least according to the documentation, command= takes a function that has no arguments. So the fact that you have command=self.foo means that you're going to end up calling self.foo() and there is no id argument.

Why are you expecting an id argument in go_to_modify_account but none of the others? Did you mean command=modify_account?

huangapple
  • 本文由 发表于 2023年6月6日 03:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定