2016-09-14 62 views
-1

你好,當我嘗試插入數據到sqlite有一個錯誤顯示和按鈕是從框架消失的問題是在這兩塊函數def fitcher(insertFun):def insertFun(self)當使用按鈕參數'命令'插入數據到sqlite

from tkinter import * 
from tkinter import ttk 
import sqlite3 

class mainGui(ttk.Frame): 
    def __init__(self): 
     Frame.__init__(self,background="lightblue") 
     self.master.title("Family book library") 
     self.pack(expand=1, fill=BOTH) 
     toolBar = Frame(self) 
     self.buttonPicInsert = PhotoImage(file="insert.png") 
     addbook = ttk.Button(toolBar,image = self.buttonPicInsert,command = self.insertFun) 
     addbook.pack(side=LEFT) 

     self.buttonPicAnalyse = PhotoImage(file="analys.png") 
     analysis = ttk.Button(toolBar, image = self.buttonPicAnalyse,command = self.analysFun) 
     analysis.pack(side=LEFT) 

     self.buttonPicSearch = PhotoImage(file="search.png") 
     search = ttk.Button(toolBar, image = self.buttonPicSearch) 
     search.pack(side=LEFT) 

     toolBar.pack(side=LEFT) 

     menu = Menu(self.master) 
     self.master.config(menu=menu) 
     file = Menu(menu) 
     file.add_command(label="Exit", command=quit) 
     menu.add_cascade(label="File", menu=file) 

     tree = ttk.Treeview(show="headings", height=32) 
     tree["columns"] = ("1", "2", "3", "4", "5", "6", "7" ,"8") 

     tree.column("1", width=132) 
     tree.column("2", width=302) 
     tree.column("3", width=302) 
     tree.column("4", width=142) 
     tree.column("5", width=123) 
     tree.column("6", width=120) 
     tree.column("7", width=120) 
     tree.column("8", width=120) 


     tree.heading("1", text="BookID") 
     tree.heading("2", text="BookTitle") 
     tree.heading("3", text="BookAuthor") 
     tree.heading("4", text="BookPublisher") 
     tree.heading("5", text="edition") 
     tree.heading("6", text="PublisherDate") 
     tree.heading("7", text="familyOwner") 
     tree.heading("8", text="Location") 

     tree.insert("", 0, text="BookID", values=("1A", "1b")) 

     tree.pack(side=TOP) 

    def insertFun(self): 
     new = Toplevel(pady=20) 
     new.title("insert new book") 
     new.transient() 
     new.geometry("570x370") 
     new.resizable(width=False, height=False) 

     # varible text filed #BookTitle label 
     new.BookTitle = ttk.Label(new, text="BookTitle ", justify = CENTER) 
     new.BookTitle.pack() 

     # 1 
     new.BookTitleFiled = ttk.Entry(new, width=90, justify = CENTER) 
     new.BookTitleFiled.pack() 

     # varible text filed #BookAuthor label 
     new.BookAuthor = ttk.Label(new, text="BookAuthor ", justify = CENTER) 
     new.BookAuthor.pack() 

     # 2 
     new.BookAuthorFiled = ttk.Entry(new, width=90, justify = CENTER) 
     new.BookAuthorFiled.pack() 

     # varible text filed #BookPublisher label 
     new.BookPub = ttk.Label(new, text="BookPublisher ", justify = CENTER) 
     new.BookPub.pack() 

     # 3 
     new.BookPubFiled = ttk.Entry(new, width=90, justify = CENTER) 
     new.BookPubFiled.pack() 

     # varible text filed #Edition label 
     new.Edition = ttk.Label(new, text="Edition ", justify = CENTER) 
     new.Edition.pack() 

     # 4 
     new.EditionFiled = ttk.Entry(new, width=90, justify = CENTER) 
     new.EditionFiled.pack() 

     # varible text filed #PublisherDate label 
     new.PublisherDate = ttk.Label(new, text="PublisherDate ", justify = CENTER) 
     new.PublisherDate.pack() 

     # 5 
     new.PublisherDateFiled = ttk.Entry(new, width=90, justify = CENTER) 
     new.PublisherDateFiled.pack() 

     # varible text filed #Owner label 
     new.Owner = ttk.Label(new, text="Family Owner ", justify = CENTER) 
     new.Owner.pack() 

     # 6 
     new.OwnerFiled = ttk.Entry(new, width=90, justify = CENTER) 
     new.OwnerFiled.pack() 

     # varible text filed #location label 
     new.location = ttk.Label(new, text="location ", justify = CENTER) 
     new.location.pack() 

     # 7 
     new.locationFiled = ttk.Entry(new, width=90, justify = CENTER) 
     new.locationFiled.pack() 


     new.submit = ttk.Button(new, text="insert data", command = new.fitcher) 
     new.submit.pack(side=BOTTOM,fill=X,pady=1,padx=10) 

    def analysFun(self): 
     anal = Toplevel() 
     anal.title("Wall of fam") 
     anal.transient() 
     anal.geometry("320x90+450+300") 
     anal.resizable(width=False, height=False) 
     anal.nsum = Label(anal, text="NumberOfBook") 
     anal.nsum.grid(sticky='nw',row=1, column=0) 
     anal.nowner = Label(anal, text="NumberOfOwner") 
     anal.nowner.grid(sticky='nw',row=2, column=0) 
     anal.nowner = Label(anal, text="OwnerName") 
     anal.nowner.grid(sticky='nw', row=3, column=0) 
     anal.locations = Label(anal, text="LocationDistribution") 
     anal.locations.grid(sticky='nw',row=4, column=0) 

    def fitcher(insertFun): 
     conn = sqlite3.connect('books.sqlite') 
     c = conn.cursor() 
     BookTitleValue = insertFun.BookTitleFiled.get() 
     BookAuthorValue = insertFun.BookAuthorFiled.get() 
     BookPublisherValue = insertFun.BookPubFiled.get() 
     editionValue = insertFun.EditionFiled.get() 
     PublisherDateValue = insertFun.PublisherDateFiled.get() 
     familyOwnerValue = insertFun.OwnerFiled.get() 
     LocationValue = insertFun.locationFiled.get() 

     c.execute("INSERT INTO library (BookTitle, BookAuthor, BookPublisher, edition, PublisherDate,familyOwner,Location) values (? , ?, ?, ?, ?, ?, ?)", 
        (BookTitleValue, BookAuthorValue, BookPublisherValue, editionValue, PublisherDateValue, familyOwnerValue, LocationValue)) 



if __name__ == "__main__": 
    mainGui().mainloop() 

錯誤消息是向下跌破

C:\Users\issba\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/issba/Desktop/workstation/familyLibrary/mainGui.py 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\issba\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__ 
    return self.func(*args) 
    File "C:/Users/issba/Desktop/workstation/familyLibrary/mainGui.py", line 121, in insertFun 
    new.submit = ttk.Button(new, text="insert data", command = new.fitcher) 
AttributeError: 'Toplevel' object has no attribute 'fitcher' 

Process finished with exit code 0 
+0

嘗試改變'new.fitcher'到'self.fitcher'。 – acw1668

+0

AttributeError:'mainGui'對象沒有屬性'BookTitleFiled這是錯誤信息的一部分 – keko

+0

將'self.fitcher'改爲'lambda x = new:self.fitcher(x)'和函數'def fitcher(insertFun)' 'def fitcher(self,insertFun)'。 – acw1668

回答

1

你缺少了很多東西在這裏就我看到 首先你需要這個功能

new.wait_window() 
return (new.varb..etc) 

,你需要添加此

new.conn.commit() 
new.conn.close() 

此功能,您必須更改縮進把它拿出這個功能__init__

def insertFun(): 

你必須使用這個功能來取數據

textvariable = new.locationGet 

例如

new.locationGet = tk.StringVar() 
new.locationFiled = ttk.Entry(new, width=90, justify = CENTER,textvariable = new.locationGet) 
new.locationFiled.pack() 

也改變了表彰只是閱讀此頁就很您的問題here

+0

謝謝你,我從相同的鏈接,但它在這個網站 – keko

+0

@isslam_akkilah非常感謝你 – keko