社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

类继承和导入——python

Nicholas Renard • 4 年前 • 1576 次点击  

这可能有点长,但我对导入父类和子类感到困惑。

我有一种感觉,在应用类名时,我的大写字母不正确。我得到了一个错误:

TypeError: Student.__init__() missing 2 required positional arguments: 'age' and 'major'

非常感谢您的帮助。

学生资料。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()

人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)

大学生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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131433
文章 [ 1 ]  |  最新文章 4 年前