链接 tkinter 和 sqlite3 [英] Linking tkinter and sqlite3

查看:31
本文介绍了链接 tkinter 和 sqlite3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新的 Python 程序员,我正在使用 tkinter 和 sqlite3 创建一个电话簿应用程序,但我遇到了一个问题.该应用程序运行没有问题,但不插入或删除数据,尽管它打印(成功插入)并成功删除).请为您提供帮助.代码如下,提前致谢:

I'm a new python programmer and I as creating a phonebook app using tkinter and sqlite3 but I faced a problem. The app runs without problem but not inserting or deleting data although it prints (inserted successfully) and deleted successfully). Please for you kind assistance. The code is below and thanks in advance:

from tkinter import*
import tkinter as tk
from tkinter import ttk 
import sqlite3

class Phonebook2:   
def __init__(self, master):

    lblName = ttk.Label(top, text="Name:")
    lblName.grid(row = 0, column = 0, sticky=W)
    lblPhone = ttk.Label(top, text="Phone number:")
    lblPhone.grid(row=1, column=0, sticky=W)

    self.name= StringVar()
    self.txtName = Entry(top, textvariable=self.name)
    self.txtName.grid(row=0, column=1, padx=0, pady=0, sticky=EW, columnspan=19)

    self.num= IntVar()
    self.txtPhone = Entry(top, textvariable=self.num)
    self.txtPhone.grid(row=1, column=1, sticky=EW)


    btnAdd = ttk.Button(top, text="Add", command=self.insert)
    btnAdd.grid(row=0, column=20, sticky=EW, padx=2, pady=2)
    btnDelete = ttk.Button(top, text="Delete", command=self.delete)
    btnDelete.grid(row=1, column=20, sticky=EW, padx=2)
    btnClear = ttk.Button(top, text="Clear", command=self.clear)
    btnClear.grid(row=2, column=20, sticky=EW, padx=2)
    btnSearch = ttk.Button(top, text="Search")
    btnSearch.grid(row=3, column=20, sticky=EW, padx=2)
    btnExit = ttk.Button(top, text="Exit", command = exit)
    btnExit.grid(row=4, column=20)


def exit(self):
    top.destroy()


def insert(self):

    NM = self.txtName.get()
    PH = self.txtPhone.get()
    conn = sqlite3.connect ('Phonebook.db')
    print ("connection opened for insertion...")
    cursor = conn.cursor()
    cursor.execute ("""INSERT INTO Phonebook (NAME, PHONE_NUMBER) VALUES (?, ?)""", (NM, PH))
    conn.commit()
    conn.close()
    print ('Inserted successfully')


def delete(self):
    conn = sqlite3.connect('Phonebook.db')
    print ("Connection opened for deletion...")
    cursor = conn.cursor()
    cursor.execute ("DELETE FROM Phonebook")
    conn.commit()
    conn.close()
    print ("All records are deleted successfully...")


def clear(self):
    self.txtName.delete(0, END)
    self.txtPhone.delete(0, END)


top = Tk()
top.overrideredirect(True)
top.eval('tk::PlaceWindow . center')
top.title("PhoneBook2")
application = Phonebook2(top)

top.mainloop()

推荐答案

def del_something():

    conn = sqlite3.connect("Phonebook.db")
    cur = conn.cursor()

    cur.execute("DELETE FROM phonebook WHERE oid = " + del_ent.get()) # <- change entry
    del_ent.delete(0, END) # clear entry

    conn.commit()
    conn.close()

这篇关于链接 tkinter 和 sqlite3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆