Tkinter GUI有输入,但是当我在其中添加变量时数据库为空

时间:2019-01-30 14:13:49

标签: python tkinter sqlite

我正在创建汽车零件组织系统,并在该程序中添加了客户信息部分。但是,本节以前工作过,但是现在我添加了一个主菜单,并且该程序没有向数据库添加任何内容,只有三个{},{},{}才应该有数据

我尝试更改变量名称并使用全局变量,但仍然遇到相同的错误

from tkinter import *
import DATABASE1

def get_selected_row(event):
    global selected_tuple
    index = listbox.curselection()[0]
    selected_tuple=listbox.get(index)
    Entry1.delete(0,END)
    Entry1.insert(END,selected_tuple[1])
    Entry2.delete(0,END)
    Entry2.insert(END,selected_tuple[2])
    Entry3.delete(0,END)
    Entry3.insert(END,selected_tuple[3])

def View_Command():
    listbox.delete(0,END)
    for row in DATABASE1.View():
        listbox.insert(END,row)

def Add_Command():
    DATABASE1.Insert(FirstName.get(),LastName.get(),PhoneNum.get())
    listbox.delete(0,END)
    listbox.insert(END,(FirstName.get(),LastName.get(),PhoneNum.get()))
    View_Command()

def Delete_Command():
    DATABASE1.Delete((selected_tuple[0]))
    View_Command()

def Update_Command():

DATABASE1.Update(selected_tuple [0],FirstName.get(),LastName.get(),PhoneNum.get())         View_Command()

def OpenCustomerInfo():
    global listbox
    global FirstName
    global LastName
    global PhoneNum
    global index
    global Entry1
    global Entry2
    global Entry3
    global scroll

    CustomerInfo = Tk()
    CustomerInfo.title("Customer Information")
    CustomerInfo.iconbitmap(r'favicon.ico')

    Label1 = Label(CustomerInfo,text="Customer Forename")
    Label1.grid(row = 0, column = 0)

    Label2 = Label(CustomerInfo,text="Customer Surname")
    Label2.grid(row = 1, column = 0)

    Label3 = Label(CustomerInfo,text="Phone Number")
    Label3.grid(row = 2, column = 0)

    FirstName = StringVar()
    Entry1 = Entry(CustomerInfo,textvariable=FirstName)
    Entry1.grid(row = 0, column = 1)

    LastName = StringVar()
    Entry2 = Entry(CustomerInfo,textvariable=LastName)
    Entry2.grid(row = 1, column = 1)

    PhoneNum = StringVar()
    Entry3 = Entry(CustomerInfo,textvariable=PhoneNum)
    Entry3.grid(row = 2, column = 1)

    listbox = Listbox(CustomerInfo, height=20,width=50)
    listbox.grid(row = 0, column = 4, rowspan = 6, columnspan = 2)

    scroll=Scrollbar(CustomerInfo)
    scroll.grid(row = 0, column = 2, sticky = NS, rowspan=8)

    listbox.configure(yscrollcommand=scroll.set)
    scroll.configure(command=Listbox.yview)

    listbox.bind('<<ListboxSelect>>',get_selected_row)

    b1 = Button(CustomerInfo, text = "Add To Database", width = 17, command = Add_Command)
    b1.grid(row = 3, column = 0, sticky = S)

    b2 = Button(CustomerInfo, text = "Delete From Database", width = 17, command = Delete_Command)
    b2.grid(row = 4, column = 0, sticky = N)

    b3 = Button(CustomerInfo, text = "Update Database", width = 17, command = Update_Command)
    b3.grid(row = 3, column = 1, sticky = S)

    b4 = Button(CustomerInfo, text = "View Database", width = 17, command = View_Command)
    b4.grid(row = 4, column = 1, sticky = N)

def MainMenu():
    MainMenu = Tk()
    MainMenu.title("Main Menu")
    Label1 = Label(MainMenu, text = "Main Menu")
    Label1.grid(row = 0, column = 0)
    Button1 = Button(MainMenu, text = "Open Customer Information Page",command = OpenCustomerInfo)
    Button1.grid(row = 1, column = 0)

MainMenu()

这是我的数据库文件。

import sqlite3

def Connect():
    connection = sqlite3.connect("Customers.db")
    cursor = connection.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS Customers (ID INTEGER PRIMARY KEY, FIRSTNAME VARCHAR(15), LASTNAME VARCHAR(15), PHONENUM INTEGER)")
    connection.commit()
    connection.close()

def Insert(FIRSTNAME,LASTNAME,PHONENUM):
    connection = sqlite3.connect("Customers.db")
    cursor = connection.cursor()
    cursor.execute("INSERT INTO Customers VALUES(NULL,?,?,?)",(FIRSTNAME,LASTNAME,PHONENUM,))
    connection.commit()
    connection.close()
    View()

def View():
    connection = sqlite3.connect("Customers.db")
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM Customers")
    row = cursor.fetchall()
    connection.close()
    return row

def Delete(ID):
    connection = sqlite3.connect("Customers.db")
    cursor = connection.cursor()
    cursor.execute("DELETE FROM Customers WHERE ID=?",(ID,))
    connection.commit()
    connection.close()

def Update(ID,FIRSTNAME,LASTNAME,PHONENUM):
    connection = sqlite3.connect("Customers.db")
    cursor = connection.cursor()
    cursor.execute("UPDATE Customers SET FIRSTNAME = ? , LASTNAME = ? , PHONENUM = ? WHERE ID = ?",(FIRSTNAME,LASTNAME,PHONENUM,ID))    
    connection.commit()
    connection.close()

Connect()

从GUI的输入框中获取输入时,应将数据添加到数据库中,然后显示更新的数据库,但应添加三个空元素,例如{},{},{}

0 个答案:

没有答案