无法从文本标签中读取数据并存储在数据库中

时间:2017-07-29 08:54:12

标签: python tkinter sqlite

请指导我,我是python的新手。我正在添加库管理器部分创建库管理项目我正在插入所有必需的信息但无法将数据存储在数据库中。 根据我的想法,我无法从下面的标签中读取数据是代码

import tkinter as tk
from tkinter.messagebox import showinfo
import sqlite3


LARGE_FONT= ("Verdana", 12)


def addentry() :

       db = sqlite3.connect("LibManagment.db")
       cur=db.cursor()
       Name = str()
       Password = str()
       Email = str()
       Address = str()
       City = str()
       Contect = int()
       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")
def viewentry() :

       db = sqlite3.connect("LibManagment.db")
       cursor=db.cursor()
       cursor.execute('SELECT * FROM Add_lib2')
       for row in cursor.fetchall():
           print(row)
       db.commit()

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


        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)
            namevar = tk.StringVar()
            Libname_lable = tk.Entry(self, textvariable=namevar)
            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)
            pwdvar = tk.StringVar()
            Libpass_label = tk.Entry(self, show ='*', textvariable=pwdvar)
            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)
            emailvar = tk.StringVar()
            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)
            addressvar = tk.StringVar()
            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)
            cityvar = tk.StringVar()
            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)
            contectvar =tk.StringVar()
            Libcontect_label = tk.Entry(self, textvariable=contectvar)
            Contect = contectvar.get()
            Libcontect_label.pack(pady=10,padx=10)

            button1 = tk.Button(self, text="Add",
                                command=addentry)

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

            button4.pack()







app = Myproj()
app.mainloop()

我在数据库中存储数据时遇到问题,我在标签文件中插入数据库。

1 个答案:

答案 0 :(得分:0)

您正在创建窗口小部件后立即读取窗口小部件值:

        namevar = tk.StringVar()
        Libname_lable = tk.Entry(self, textvariable=namevar)
        Name = namevar.get()

此时,小部件仍为空。

您必须在执行INSERT之前读取该值。 (在那个地方,你用空值覆盖一切都没有用。)

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