2016-09-14 4 views
-1

こんにちはsqliteにデータを挿入しようとするとエラーが表示され、ボタンがフレームから消えてしまう問題とdef insertFun(self)私は を見るようにボタンの引数 'command'を使用して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

だ、このページを読んで称賛を変更。 – 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() 

もちょうどそれがself.fitcher` `に` new.fitcher`を変更してみてください非常に問題にhere

+0

ありがとう、私は同じリンクからそれをしましたが、このサイトにありました – keko

+0

@isslam_akkilahありがとう – keko

関連する問題