-1

I have declared variable inside the class but now I am getting below error

Traceback (most recent call last): 
File "ank.py", line 162, in <module> class Addlib(tk.Frame): File "ank.py", 
line 165, in Addlib self.Name = tk.StringVar() 
File "/usr/lib/python3.5/tkinter/__init__.py", line 337, in __init__ Variable.__init__(self, master, value, name) 
File "/usr/lib/python3.5/tkinter/__init__.py",
line 236, in __init__ self._root = master._root() AttributeError: 
'NoneType' object has no attribute '_root'

Below is my code I have declared variable inside class and when i ran the code i am getting above Error please guide me further so that i can resolve problem basically i have to store the data in table from text box.

import tkinter as tk
from tkinter.messagebox import showinfo

import sqlite3
LARGE_FONT= ("Verdana", 12)

class Myproj(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, Adminlogin, Liblogin, Adsection, Addlib):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Library Managment system", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = tk.Button(self, text="Admin Login",
                            command=lambda: controller.show_frame(Adminlogin))
        button.pack()

        button2 = tk.Button(self, text="Lib Login",
                            command=lambda: controller.show_frame(Liblogin))
        button2.pack()


class Adminlogin(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        name_label = tk.Label(self, text="User ID:", font=LARGE_FONT)
        name_label.pack(pady=10,padx=10)
        name_lable = tk.Entry(self)
        name_lable.pack(pady=10,padx=10)
        pwd_label = tk.Label(self, text="Password", font=LARGE_FONT)
        pwd_label.pack(pady=10,padx=10)
        pwd_lable = tk.Entry(self, show="*")
        pwd_lable.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = tk.Button(self, text="Login",
                            command=lambda: controller.show_frame(Adsection))
        button2.pack()


class Liblogin(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        Lname_label = tk.Label(self, text="User ID:", font=LARGE_FONT)
        Lname_label.pack(pady=10,padx=10)
        Lname_lable = tk.Entry(self)
        Lname_lable.pack(pady=10,padx=10)
        Lpwd_label = tk.Label(self, text="Password", font=LARGE_FONT)
        Lpwd_label.pack(pady=10,padx=10)
        Lpwd_lable = tk.Entry(self, show="*")
        Lpwd_lable.pack(pady=10,padx=10)


        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = tk.Button(self, text="Login",
                            command=lambda: controller.show_frame(Adminlogin))
        button2.pack()

class Adsection(tk.Frame):



        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)

            button1 = tk.Button(self, text="Add Librarian",
                                command=lambda: controller.show_frame(Addlib))
            button1.pack()

            button2 = tk.Button(self, text="View Librarian",
                                command=viewentry)
            button2.pack()

            button3 = tk.Button(self, text="Delete Librarian",
                                command=lambda: controller.show_frame(StartPage))
            button3.pack()

            button4 = tk.Button(self, text="Logout",
                                command=lambda: controller.show_frame(StartPage))
            button4.pack()



class Addlib(tk.Frame):
         Name = tk.StringVar()
         Password = tk.StringVar()
         emailvar = tk.StringVar()
         addressvar = tk.StringVar()
         cityvar = tk.StringVar()
         contectvar =tk.StringVar()

        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            Libname_label = tk.Label(self, text="Name:", font=LARGE_FONT)
            Libname_label.pack(pady=10,padx=10)
            Libname_lable = tk.Entry(self, textvariable=Name)
            #Name = namevar.get()
            Libname_lable.pack(pady=10,padx=10)
            Libpass_label = tk.Label(self, text="Password:", font=LARGE_FONT)
            Libpass_label.pack(pady=10,padx=10)
            Libpass_label = tk.Entry(self, show ='*', textvariable=Password)
            #Password = pwdvar.get()
            Libpass_label.pack(pady=10,padx=10)
            Libemail_label = tk.Label(self, text="Email:", font=LARGE_FONT)
            Libemail_label.pack(pady=10,padx=10)
            Libemail_label = tk.Entry(self, textvariable=emailvar)
            #Email = emailvar.get()
            Libemail_label.pack(pady=10,padx=10)
            LibAddres_label = tk.Label(self, text="Address:", font=LARGE_FONT)
            LibAddres_label.pack(pady=10,padx=10)
            LibAddres_label = tk.Entry(self, textvariable=addressvar)
            #Address = addressvar.get()
            LibAddres_label.pack(pady=10,padx=10)
            Libcity_label = tk.Label(self, text="City:", font=LARGE_FONT)
            Libcity_label.pack(pady=10,padx=10)
            Libcity_label = tk.Entry(self, textvariable=cityvar)
            #City = cityvar.get()
            Libcity_label.pack(pady=10,padx=10)
            Libcontect_label = tk.Label(self, text="Contect:", font=LARGE_FONT)
            Libcontect_label.pack(pady=10,padx=10)
            Libcontect_label = tk.Entry(self, textvariable=contectvar)
            #Contect = contectvar.get()
            Libcontect_label.pack(pady=10,padx=10)

            button1 = tk.Button(self, text="Show",command=show)
            button1.pack()
            #print("City name is {}".format(cityvar))
            button4 = tk.Button(self, text='BACK',
                                command=lambda: controller.show_frame(Adsection))

            button4.pack()

        def addentry() :

            db = sqlite3.connect("LibManagment.db")
            cur.execute('INSERT INTO Add_lib2 VALUES (?, ?, ?, ?, ?, ?);', (Name, Password, Email, Address, City, Contect))
            print("Entry Added To Database")
            db.commit()
            showinfo( title = "Librarian Add", message = "Data inserted To table")


app = Myproj()
app.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Tathe
  • 3
  • 2
  • Welcome to StackOverflow! please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask). Asking question properly will help you get better answers and help others be able to understand your question if they have a similar problem. Also please provide [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Mike - SMT Aug 02 '17 at 12:59
  • It looks like a duplicate of [this](https://stackoverflow.com/questions/22315899/tkinter-stringvar-not-working-correctly) – Gribouillis Aug 02 '17 at 13:03

1 Answers1

2

You have several problems with your code. 1 major problem is your indention. You need to fix that first.

Next thing I noticed is how you are trying to declare some of your variables.

class Addlib(tk.Frame):
         Name = tk.StringVar()
         Password = tk.StringVar()
         emailvar = tk.StringVar()
         addressvar = tk.StringVar()
         cityvar = tk.StringVar()
         contectvar =tk.StringVar()

        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)

You need to move all your variables into the __init__ section as this is where the class attributes are instantiated. Also you need to add self. prefix to all your variables here to make them class attributes because you have methods in your class that need to use these variables and you interact with them more than once in the program.

Your class should start like this:

class Addlib(tk.Frame):


    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.Name = tk.StringVar()
        self.Password = tk.StringVar()
        self.emailvar = tk.StringVar()
        self.addressvar = tk.StringVar()
        self.cityvar = tk.StringVar()
        self.contectvar = tk.StringVar()

Another issue I noticed is you have buttons with commands that point to non-existent methods. In the Eclipse IDE this will prevent the code from running and I would imagine it would do the same on other IDE's.

I would suggest using a pass method as a placeholder for button commands that are not yet in use. At least I like to do it this way so I can continue testing my code without all the methods/functions needing to be written in yet.

Something like the below:

def do_nothing(self):
    pass

this can be used in any command like command = self.do_nothing this way you can have your buttons all set up until you make the methods/functions you need later in your program.

One last thing is this line:

cur.execute('INSERT INTO Add_lib2 VALUES (?, ?, ?, ?, ?, ?);', (self.Name, self.Password, self.Email, self.Address, self.City, self.Contect))

There is nothing in your code that matches cur so this line I had to comment out to allow the program to run. You may need to verify this section will work.

With the few changes I mentioned above your code runs fine.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79