2017-07-29 88 views
0

請指導我我是新的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() 

+0

請編輯您的帖子糾正語法。在Python中,縮進非常重要,目前它是一團糟;-) - 粘貼代碼,突出顯示它並使用Ctrl + K鍵縮進整個塊。 – Maurice

+0

你甚至沒有試圖從標籤讀取數據。 –

+0

感謝您的輸入在Addlib類中,我通過使用get方法讀取數據,但是我認爲如果可能的話,我在數據庫中存儲數據時會出現一些錯誤,請您提出建議,以便它可以工作 – Tathe

回答

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)) 
+0

再次感謝指出我的錯誤。你可以讓我知道我怎麼可以使用裏面的類變量namevar存儲在數據庫中,當我使用它我越來越錯誤: ameError:名稱'namevar'未定義 – Tathe

+0

您可以使用*班級*,即在該班級的任何方法中。再次 –

+0

感謝您的輸入我已經聲明裏面的類變量,但現在我得到以下錯誤 '回溯(最近最後一次通話): 文件「ank.py」,線路162,在 類Addlib(TK。框架): 文件「ank.py」,行165,在Addlib中 self.Name = tk.StringVar() 文件「/usr/lib/python3.5/tkinter/__init__.py」,行337,位於__init__中 變量.__ init __(self,master,value,name) 文件「/usr/lib/python3.5/tkinter/__init__.py」,第236行,在__init__中 self._root = master._root() AttributeError: 'NoneType'對象沒有屬性'_root'' – Tathe

相關問題