实验八 Tkinter应用

——2020.11.25
- 将实验七设计的学生通讯录实现界面操作。
import sqlite3
import tkinter as tk
import tkinter.messagebox
from tkinter import *
conn = sqlite3.connect('student.db')
sql = conn.cursor()
#sql.execute('CREATE TABLE stuconnect (stuid integer primary key, stuname varchar(20),address varchar(30),tel varchar(15))')
def addview():
windowadd = tk.Tk()
windowadd.title('新增学生通讯信息')
windowadd.geometry('500x300')
l = tk.Label(windowadd, text='新增学生通讯信息', font=('Arial', 16), width=500, height=2)
l.pack()
l1 = tk.Label(windowadd, text='姓名', font=('Arial', 10), width=100, height=2)
l2 = tk.Label(windowadd, text='地址', font=('Arial', 10), width=100, height=2)
l3 = tk.Label(windowadd, text='电话', font=('Arial', 10), width=100, height=2)
e1 = tk.Entry(windowadd, show=None, font=('Arial', 14))
e2 = tk.Entry(windowadd, show=None, font=('Arial', 14))
e3 = tk.Entry(windowadd, show=None, font=('Arial', 14))
l1.pack()
e1.pack()
l2.pack()
e2.pack()
l3.pack()
e3.pack()
def add():
name = e1.get()
address = e2.get()
tel = e3.get()
flag = 0
flag = sql.execute('INSERT INTO stuconnect(stuname,address,tel) VALUES (?,?,?)', (name, address, tel))
if(flag!=0):
tk.messagebox.showinfo('提示', '添加成功')
else:
tk.messagebox.showinfo('提示', '添加失败')
buadd = tk.Button(windowadd, text='添加', font=('Arial', 12), width=10, height=1, command=add)
buadd.pack()
windowadd.mainloop()
def dellview():
windowdel = tk.Tk()
windowdel.title('删除学生通讯信息')
windowdel.geometry('500x300')
l = tk.Label(windowdel, text='删除学生通讯信息', font=('Arial', 16), width=500, height=2)
l.pack()
l1 = tk.Label(windowdel, text='姓名', font=('Arial', 10), width=100, height=2)
e1 = tk.Entry(windowdel, show=None, font=('Arial', 14))
l1.pack()
e1.pack()
def dell():
name = e1.get()
alist = sql.execute("SELECT * FROM stuconnect WHERE stuname='" + name + "'")
if (len(list(alist))):
flag = 0
flag = sql.execute("DELETE FROM stuconnect WHERE stuname='" + name + "'")
if (flag != 0):
tk.messagebox.showinfo('提示', '删除成功')
else:
tk.messagebox.showinfo('提示', '删除失败')
else:
tk.messagebox.showinfo('提示', '该学生不存在')
budel = tk.Button(windowdel, text='删除', font=('Arial', 12), width=10, height=1, command=dell)
budel.pack()
windowdel.mainloop()
def editview():
windowedit = tk.Tk()
windowedit.title('修改学生通讯信息')
windowedit.geometry('500x300')
l = tk.Label(windowedit, text='删除学生通讯信息', font=('Arial', 16), width=500, height=2)
l.pack()
l1 = tk.Label(windowedit, text='姓名', font=('Arial', 10), width=100, height=2)
e = tk.Entry(windowedit, show=None, font=('Arial', 14))
l1.pack()
e.pack()
def edit1():
name1 = e.get()
alist = sql.execute("SELECT * FROM stuconnect WHERE stuname='" + name1 + "'")
if (len(list(alist))):
l1 = tk.Label(windowedit, text='新姓名', font=('Arial', 10), width=100, height=2)
l2 = tk.Label(windowedit, text='新地址', font=('Arial', 10), width=100, height=2)
l3 = tk.Label(windowedit, text='新电话', font=('Arial', 10), width=100, height=2)
e1 = tk.Entry(windowedit, show=None, font=('Arial', 14))
e2 = tk.Entry(windowedit, show=None, font=('Arial', 14))
e3 = tk.Entry(windowedit, show=None, font=('Arial', 14))
l1.pack()
e1.pack()
l2.pack()
e2.pack()
l3.pack()
e3.pack()
def edit():
name = e1.get()
address = e2.get()
tel = e3.get()
flag = 0
flag = sql.execute('UPDATE stuconnect SET stuname=?,address=?,tel=? WHERE stuname=?',
(name, address, tel, name1))
if (flag != 0):
tk.messagebox.showinfo('提示', '修改成功')
else:
tk.messagebox.showinfo('提示', '修改失败')
else:
tk.messagebox.showinfo('提示', '该学生不存在')
buedit = tk.Button(windowedit, text='修改', font=('Arial', 12), width=10, height=1, command=edit)
buedit.pack()
buedit1 = tk.Button(windowedit, text='查询', font=('Arial', 12), width=10, height=1, command=edit1)
buedit1.pack()
windowedit.mainloop()
def searchview():
alist = sql.execute('SELECT * FROM stuconnect')
description = alist.description
rows = alist.fetchall()
windowsearch = tk.Tk()
windowsearch.title('学生通讯信息')
windowsearch.geometry('500x300')
for i, col in enumerate(description):
lb = tk.Button(windowsearch, text=col[0], padx=15, pady=6)
lb.grid(row=0, column=i)
for i, row in enumerate(rows):
for j in range(len(row)):
en = tk.Label(windowsearch, text=row[j])
en.grid(row=i+1, column=j)
windowsearch.mainloop()
window = tk.Tk()
window.title('学生通讯录')
window.geometry('500x300')
l = tk.Label(window, text='欢迎使用学生通讯录管理系统', font=('Arial', 16), width=500, height=2)
l.pack()
bu1=tk.Button(window, text='添加', font=('Arial', 12), width=10, height=1, command=addview)
bu1.pack()
bu2=tk.Button(window, text='删除', font=('Arial', 12), width=10, height=1, command=dellview)
bu2.pack()
bu3=tk.Button(window, text='修改', font=('Arial', 12), width=10, height=1, command=editview)
bu3.pack()
bu4=tk.Button(window, text='查询', font=('Arial', 12), width=10, height=1, command=searchview)
bu4.pack()
window.mainloop()
0 条评论