简体   繁体   English

将sqlite3与Tkinter链接

[英]Linking sqlite3 with Tkinter

As a part of an inventory system I am making, I want Tkinter to collect the values of the data I wish to insert into a database through the user typing in entry boxes and clicking an 'Add Stock' button. 作为我正在制作的库存系统的一部分,我希望Tkinter通过用户输入输入框并单击“添加库存”按钮来收集希望插入数据库的数据值。

My problem is, that, well I'm not entirely sure how to combine sqlite3 with Tkinter code. 我的问题是,我不确定如何将sqlite3与Tkinter代码结合使用。

Below is my Tkinter code for the 'Add Stock' window 以下是我的“添加库存”窗口的Tkinter代码

def addStock():
    root = Tk()
    root.title('Add Stock')
    label1 = Label(root, text='Gender')
    label2 = Label(root, text='Price')
    label3 = Label(root, text='Enter Product Name')
    label4 = Label(root, text='Colour')
    label5 = Label(root, text='Size')
    label6 = Label(root, text='Enter Amount')
    label7 = Label(root, text='Source')
    label8 = Label(root, text='Enter ProductID')
    entry1 = Entry(root)
    entry2 = Entry(root)
    entry3 = Entry(root)
    entry4 = Entry(root)
    entry5 = Entry(root)
    entry6 = Entry(root)
    entry7 = Entry(root)
    entry8 = Entry(root)

    label1.grid(row=0, sticky=E)
    label2.grid(row=1, sticky=E)
    label3.grid(row=2, sticky=E)
    label4.grid(row=3, sticky=E)
    label5.grid(row=4, sticky=E)
    label6.grid(row=5, sticky=E)
    label7.grid(row=6, sticky=E)
    label8.grid(row=7, sticky=E)

    entry1.grid(row=0, column=1)
    entry2.grid(row=1, column=1)
    entry3.grid(row=2, column=1)
    entry4.grid(row=3, column=1)
    entry5.grid(row=4, column=1)
    entry6.grid(row=5, column=1)
    entry7.grid(row=6, column=1)
    entry8.grid(row=7, column=1)

    frame3 = Frame(root)
    frame3.grid(columnspan = 2)

    button1 = Button(frame3, padx=10, pady=10, bd=2, text="Add Stock", command=insert_data)
    button1.grid()

Below is my insert statements for sqlite3 that I wish to link with the Tkinter code. 以下是我希望与Tkinter代码链接的sqlite3的insert语句。

def insert_data(values): 
    with sqlite3.connect("jam_stock.db") as db:
        cursor = db.cursor()
        sql = "insert or ignore into Product (Name, ProductID) values (?,?)"
        query(sql,values)
        db.commit()

def insert_product_type_data(records): #normalised
    sql = "insert into ProductType(AmountInStock, Size, Colour, Source) values (?,?,?,?)"
    for record in records:
        cursor.execute(sql,record)

def insert_product_gender_data(records): #normalised
    sql = "insert into ProductGender(Gender, Price) values (?,?)"
    for record in records:
        cursor.execute(sql, records)

I have already defined the tables referenced in the subroutines, but I'm struggling to find a way to insert data into those tables through Tkinter. 我已经定义了子例程中引用的表,但是我正在努力寻找一种通过Tkinter将数据插入到这些表中的方法。

I am running Python 3.4 for those wondering. 我正在为那些想知道的人运行Python 3.4。 Help would be highly appreciated. 帮助将不胜感激。

The normal way to approach this is to have your button call a function specially for that button. 解决此问题的通常方法是让您的按钮调用专门用于该按钮的功能。 The responsibilities of that function are to gather the data from the GUI, call some function that uses the data, and then post any results back to the GUI. 该功能的职责是从GUI收集数据,调用使用该数据的某些功能,然后将所有结果发布回GUI。

For example: 例如:

...
button1 = Button(... text="Add Stock", command=_on_add_stock)
...

def _on_add_stock():
    gender = entry1.get()
    price = entry2.get()
    ...
    insert_data(gender, price, ...)

The above won't work with the code you posted, but it gives the general idea. 上面的代码不适用于您发布的代码,但是它提供了大致的思路。 One reason it won't work with the code you've posted is because entry1 , etc are all local variables. 它与您发布的代码不兼容的原因之一是因为entry1等都是局部变量。 It would behoove you to use a more object-oriented approach, so those variables are all attributes of a class. 您应该使用一种更加面向对象的方法,因此这些变量都是类的属性。 You can see the answers to Best way to structure a tkinter application for examples. 您可以看到有关构建tkinter应用程序的最佳方法的答案的示例。

One trick is using ' command ' in your button. 一种技巧是在按钮中使用“ command ”。 As listed in the above comment try using the method: 如以上注释中所列,请尝试使用方法:

def _add_to_db: get_data = entry.get()#This should be the text from Entry insert = *some code for inserting data to database plus get_data in \\n order to be present in your database* def _add_to_db:get_data = entry.get()#这应该是Entry insert的文本= *一些用于将数据插入数据库的代码,以\\ n的顺序将get_data出现在数据库中*

btn = Button(root, text="Place in DB", command=_add_to_db).pack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM