使用按钮参数'命令'

时间:2016-09-14 02:32:12

标签: python sqlite tkinter

你好,当我尝试向sqlite插入数据时,会出现一个错误并且按钮是从框架中消失,问题出在这两个函数def fitcher(insertFun):def insertFun(self)

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

1 个答案:

答案 0 :(得分: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