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

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

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

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

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

英文:
  1. class View_Account(tk.Frame):
  2. def __init__(self,master):
  3. tk.Frame.__init__(self,master)
  4. self.parent = master
  5. main_frame = tk.Frame(self, width=750, height=450, bg='white')
  6. main_frame.pack(fill='both', expand='yes')
  7. self.Frame1 = tk.Frame(self, width=750, height=100, bg='pink')
  8. self.Frame1.place(x=0, y=0)
  9. self.Frame2 = tk.Frame(self, width=750, height=450, bg='#E8E4C9')
  10. self.Frame2.place(x=0, y=100)
  11. self.view_acc = tk.Label(self, text='View Account', font=('Algerian', 26), bg='pink')
  12. self.view_acc.place(x=90, y=10)
  13. self.modify_acc = tk.Button(self, text='Modify Account', font=('Algerian', 13), command=self.go_to_modify_account)
  14. self.modify_acc.place(x= 40, y=60)
  15. self.delete_acc = tk.Button(self, text='Delete Account', font=('Algerian', 13), command=self.delete_account)
  16. self.delete_acc.place(x= 200, y=60)
  17. self.back_button = tk.Button(self, text='Back', font=('Algerian', 13), command=self.go_to_admin_dashboard)
  18. self.back_button.place(x= 5, y=5)
  19. columns = ('id','fullname','birthdate','address','email','contact_number','card_number','pin','initial_deposit')
  20. self.table = ttk.Treeview(self, columns=columns, show='headings', selectmode='browse')
  21. self.table.heading('id', text='id')
  22. self.table.heading('fullname', text='Name')
  23. self.table.heading('birthdate', text='Birthdate')
  24. self.table.heading('address', text='Address')
  25. self.table.heading('email', text='Email')
  26. self.table.heading('contact_number', text='Contact #')
  27. self.table.heading('card_number', text='Card Number')
  28. self.table.heading('pin',text='PIN')
  29. self.table.heading('initial_deposit',text='Initial Deposit')
  30. self.table.column('id', width=5)
  31. self.table.column('fullname', width=70)
  32. self.table.column('birthdate', width=70)
  33. self.table.column('address', width=115)
  34. self.table.column('email', width=145)
  35. self.table.column('contact_number', width=80)
  36. self.table.column('card_number', width=115)
  37. self.table.column('pin', width=50)
  38. self.table.column('initial_deposit', width=100)
  39. self.table.place(x=0, y=102)
  40. self.table.configure(height=16)
  41. self.update_table()
  42. def update_table(self):
  43. self.client_account = self.get_client_account()
  44. self.table.delete(*self.table.get_children())
  45. for client in self.client_account:
  46. row = (
  47. client.id,
  48. client.fullname,
  49. client.birthdate,
  50. client.address,
  51. client.email,
  52. client.contact_number,
  53. client.card_number,
  54. client.pin,
  55. client.initial_deposit
  56. )
  57. self.table.insert('', tk.END, values=row)
  58. def get_client_account(self):
  59. db_conn = database_handler.DBHandler()
  60. self.client_account = db_conn.view_accounts()
  61. db_conn.close()
  62. return self.client_account
  63. def delete_account(self):
  64. if len(self.table.selection()) == 0:
  65. messagebox.showerror('Error', 'Select Account to delete.')
  66. else:
  67. proceed = messagebox.askyesno('Account Deletion', 'Do you wish to continue?')
  68. if not proceed:
  69. return
  70. for selected_row in self.table.selection():
  71. row = self.table.item(selected_row)
  72. id = row['values'][0]
  73. db_conn = database_handler.DBHandler()
  74. db_conn.delete_accounts(id)
  75. db_conn.close()
  76. self.update_table()
  77. def go_to_modify_account(self, id):
  78. self.parent.change_window('Modify_Account', id=id)
  79. def modify_account(self):
  80. if not self.table.selection():
  81. messagebox.showinfo('Update Account', 'Please select an account to modify')
  82. else:
  83. for selected_row in self.table.selection():
  84. row = self.table.item(selected_row)
  85. id = row['values'][0]
  86. self.go_to_modify_account(id)
  87. def go_to_admin_dashboard(self):
  88. self.parent.change_window('Admin_Dashboard')
  89. def on_return(self):
  90. 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:

确定