這可能有點長,但我在匯入我的父類和子類時不知所措。
我感覺在應用類名時我的大小寫不正確。我收到錯誤訊息:
TypeError: Student.__init__() missing 2 required positional arguments: 'age' and 'major'
任何幫助深表感謝。
StudentAdder.py(主要)
import pickle
from sre_constants import SUCCESS
from tkinter import *
from tkinter import messagebox
from student import Student
from person import Person
#main method
def main():
studentList = []
#load the list from a pickle file
with open("student.pickle", "rb") as f:
studentList = pickle.load(f)
#addStudent method to append Input to list
def addStudent():
name = "name"
age = "age"
major = "major"
name = nameInput.get()
age = ageInput.get()
major = majorInput.get()
studentList.append(Student(name, age, major))
#save list to pickle file
with open("student.pickle", "wb") as f:
pickle.dump(studentList, f)
#displays a success message upon adding a student
def onClick():
messagebox.showinfo("SUCCESS", "Student Added!")
#create a GUI
window = Tk()
window.title("Add a Student")
window.geometry("325x175")
#create labels and text boxes for each value
nameLabel = Label(window, text="Name:")
ageLabel = Label(window, text="Age:")
majorLabel = Label(window, text="Major:")
nameInput = StringVar()
ageInput = StringVar()
majorInput = StringVar()
#create grid for labels
nameLabel.grid(row=0, column=0)
ageLabel.grid(row=1, column=0)
majorLabel.grid(row=2, column=0)
#create input boxes for each value
nameInput = Entry(window, width = 25)
ageInput = Entry(window, width = 25)
majorInput = Entry(window, width = 25)
#create grid for input boxes
nameInput.grid(row=0, column=1)
ageInput.grid(row=1, column=1)
majorInput.grid(row=2, column=1)
#create a save button to save the student -- binds commands to button
saveButton = Button(window, text="Save", command= lambda: [addStudent(), onClick()])
saveButton.grid(row=3, column=1)
#create quit button
quitButton = Button(window, text="Quit", command= window.destroy)
quitButton.grid(row=4, column=1)
#start the GUI
window.mainloop()
#print the list
print()
for student in studentList:
print(student)
print()
main()
person.py(父類)
#create a parent class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def getName(self):
return self.name
def getAge(self):
return self.age
def setName(self, name):
self.name = name
def setAge(self, age):
self.age = age
def __str__(self):
return "Person: " self.name " " str(self.age)
student.py(兒童班)
from person import Person
class Student(Person):
def __init__(self, name, age, major):
Student(Person).__init__(self, name, age, major)
self.name = name
self.age = age
self.major = major
def getMajor(self):
return self.major
def setMajor(self, major):
self.major = major
def __str__(self):
return "Student: " self.name \
", Age: " str(self.age) \
", Major: " self.major
uj5u.com熱心網友回復:
看起來你的錯誤是 student.py 中的這一行:
Student(Person).__init__(self, name, age, major)
代碼Student(Person)試圖創建一個新的 Student 實體,但您只傳遞了一個引數 ( Person) 而不是必需的三個 ( name,age和major)。這就是您missing 2 required positional arguments: 'age' and 'major'在錯誤訊息中看到的原因。
但是,看起來您實際上是在嘗試呼叫__init__父類的方法。你會這樣做:
super().__init__(name, age)
請注意,我沒有指定major引數,因為Person該類不接受它作為引數。此外,該類的__init__方法Person已經存盤了name和age引數,因此您不需要在Student.__init__. 因此,您可以更改Student.__init__為以下內容:
class Student(Person):
def __init__(self, name, age, major):
super().__init__(name, age)
self.major = major
或者,如果您不想依賴Person.__init__,則可以省略super()呼叫并自己設定所有屬性:
class Student(Person):
def __init__(self, name, age, major):
self.name = name
self.age = age
self.major = major
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/457929.html
上一篇:Xcode13.3不鏈接情節提要
下一篇:Php如果函式在類外被呼叫
