我正在尝试创建一个程序,该程序允许用户将现有学生添加到现有团队中。当我尝试执行此操作时,它会产生 AttributeError:'str'对象没有属性'studentsinTeam'。我不确定如何解决此问题。
class Student:
def __init__(self, student_name, form_class):
self.student_name = student_name
self.form_class = form_class
self.studentTeamlist = []
def addtoTeam(self, team):
if self.student_name not in team.studentsinTeam:
team.studentsinTeam.append(self.student_name)
self.studentTeamlist.append(team)
class Team:
def __init__(self, team, coach):
self.team = team
self.coach = coach
self.studentsinTeam = []
class AddtoTeamScreen:
def __init__(self):
###This forms the Window for this Screen###
self.screen2 = tk.Tk()
self.screen2.title("Add Student to Team")
self.screen2.geometry("700x400")
self.getName2 = tk.StringVar()
self.getTeam = tk.StringVar()
###Main Heading######
screen2H = tk.Label(self.screen2, text = "Add Student to Team", font=("Arial", 25))
screen2H.pack()
####Student Name######
StudentNLabel = tk.Label(self.screen2, text="Student Name: ")
StudentNLabel.pack()
student_entry = tk.Entry(self.screen2, textvariable = self.getName2)
student_entry.pack()
####Combo Box for Team Selection#####
TeamNLabel = tk.Label(self.screen2, text= "Team: ")
TeamNLabel.pack()
teamcb = ttk.Combobox(self.screen2, values= allTeamlist, height = 2, textvariable = self.getTeam)
teamcb.pack()
ATbutton = tk.Button(self.screen2, text = "Add Student", command= self.pressed2)
ATbutton.pack()
self.BCKBttn = tk.Button(self.screen2, text = "Back to Main Menu", command= self.back2)
self.BCKBttn.pack()
def back2(self):
mainscreen = MainScreen()
self.screen2.destroy()
def pressed2(self):
addName2 = self.getName2.get()
joinTeam = self.getTeam.get()
for s,t in itertools.product(allStudentlist, allTeamlist):
if s.student_name == addName2 and t.team == joinTeam:
if joinTeam not in s.studentTeamlist:
s.addtoTeam(joinTeam)
messagebox.showinfo("Success", "Successful! ADDED")
elif joinTeam in s.studentTeamlist:
messagebox.showerror("Error", "Student Already in Team")
如何解决此错误?
编辑:使用self.getTeam = tk.StringVar()更新了代码
答案 0 :(得分:0)
根据建议,您可以从外部获取joinTeam变量,它是一个字符串。
因此,在将其用作团队对象之前,必须先找到该对象。并且您已经在if s.student_name == addName2 and t.team == joinTeam:
中做到了,所以现在t-是具有joinTeam名称的团队的团队对象。只需更改:
s.addtoTeam(joinTeam)
到
s.addtoTeam(t)