Skip to content
Advertisement

Can’t define class in python [closed]

CODE::

from tkinter import *
import tkinter.messagebox


class Library:

    def __init_(self, root):
        self.root = root
        self.root.title(" Library Management System ")
        self.root.geometry("1350x750+0+0")

        MTy = StringVar()
        Ref = StringVar()
        Title = StringVar()
        fna = StringVar()
        sna = StringVar()
        Adr1 = StringVar()
        Adr2 = StringVar()
        pcd = StringVar()
        MNo = StringVar()
        BkID = StringVar()
        Bkt = StringVar()
        BkT = StringVar()
        Atr = StringVar()
        DBo = StringVar()
        Ddu = StringVar()
        sPr = StringVar()
        Lrf = StringVar()
        Dod = StringVar()
        DonL = StringVar()

        #============================================FRAMES=================================================#

        MainFrame = Frame(self.root)
        MainFrame.grid()

        Title_Frame = Frame(MainFrame, bd=2, padx=40, pady=8, bg='cadet blue', relief=RIDGE)
        Title_Frame.grid(side=TOP)

        self.lblTitle = Label(Title_Frame, font=('arial', 46, 'bold'), text=" Library Management System ")
        self.lblTitle.grid(sticky=W)

        ButtonFrame = Frame(MainFrame, bd=2, width=1350, height=100, padx=20, pady=20, bg='Cadet Blue', relief=RIDGE)
        ButtonFrame.pack(side=BOTTOM)

        FrameDetail = Frame(MainFrame, bd=0, width=1350, height=50, padx=20, relief=RIDGE)
        FrameDetail.pack(side=BOTTOM)

        DataFrame = Frame(MainFrame, bd=1, width=1300, height=400, padx=20, pady=20, relief=RIDGE)
        DataFrame.pack(side=BOTTOM)

        DataFrameLEFT = LabelFrame(DataFrame, bd=1, width=800, height=300, padx=20, relief=RIDGE,
                                   font=('arial', 12, 'bold'), text=' Library Member Info ', bg='Cadet Blue')
        DataFrameLEFT.pack(side=LEFT)

        DataFrameRIGHT = LabelFrame(DataFrame, bd=1, width=450, height=300, padx=20, pady=3, relief=RIDGE,
                                    font=('arial', 12, 'bold'), text=' Book Details ', bg='Cadet Blue')
        DataFrameRIGHT.pack(side=RIGHT)

    if __name__ == '__main__':
        root = Tk()
        application = Library(root)
        root.mainloop()

Error::

Traceback (most recent call last): File “C:/Users/Jedi/PycharmProjects/LMS/main.py”, line 5, in class Library: File “C:/Users/Jedi/PycharmProjects/LMS/main.py”, line 62, in Library application = Library(root) NameError: name ‘Library’ is not defined

Process finished with exit code 1

Advertisement

Answer

Firstly, your indentation is wrong, you have to place the if on the same indentation level as class, like:

class Library:
   .....

if __name__ == '__main__':
  ....

Secondly, but most importantly, your def __init_() has a typo, it should be __init__() with two underscore trailing and leading init, but you gave just one(trailing).

class Library:
    def __init__(self, root):
        self.root = root
.....

But even after doing all this, you will get another error at Title_Frame.grid(side=TOP), because grid() has not option side, it is the option of pack(), so change that to:

Title_Frame.pack(side=TOP)
Advertisement