【基础】练习册42-Python3_带数据库的学生管理系统

界面为:


代码如下:

# 学生信息管理系统:显示所有信息、追加信息、删除信息、查询

# 细节:清空控件内容

# 待增加功能:查询增加鼠标事件来进行修改

# Treeview

import tkinter as tk

import tkinter.messagebox

import string

import database

from tkinter import ttk

win = tk.Tk()

win.geometry('500x400+200+200')

win.title('学生管理系统')

#win.grid_rowconfigure(win.grid_size(), minsize=1)

# 界面标题

tk.Label(text='学生管理系统',

        font=('宋体', 16, 'bold'),

        fg='blue',

        justify=tk.CENTER,

        width=36).grid(row=1, column=1, columnspan=6, pady=10)

# 界面设置

label1 = tk.Label(text='学号:', justify=tk.RIGHT, width=4).grid(row=2,

                                                              column=1,

                                                              pady=5)

label2 = tk.Label(text='姓名:', justify=tk.RIGHT, width=4).grid(row=2, column=4)

label3 = tk.Label(text='电话:', justify=tk.RIGHT, width=4).grid(row=3,

                                                              column=1,

                                                              pady=5)

label4 = tk.Label(text='地址:', justify=tk.RIGHT, width=4).grid(row=3, column=4)

stu_id = tk.IntVar().set(None)

stu_na = tk.StringVar()

stu_ph = tk.IntVar().set(None)

stu_adr = tk.StringVar()

entryid = tk.Entry(win, textvariable=stu_id, width=18)

entryid.grid(row=2, column=2, columnspan=2, sticky='w')

entryna = tk.Entry(win, textvariable=stu_na, width=18)

entryna.grid(row=2, column=5, columnspan=2, sticky='w')

entryph = tk.Entry(win, textvariable=stu_ph, width=18)

entryph.grid(row=3, column=2, columnspan=2, sticky='w')

entryadr = tk.Entry(win, textvariable=stu_adr, width=18)

entryadr.grid(row=3, column=5, columnspan=2, sticky='w')

tree = ttk.Treeview(win, show='headings', columns=('学号', '姓名', '电话', '地址'))

# 定义列

tree.column('学号', width=100)

tree.column('姓名', width=100)

tree.column('电话', width=100)

tree.column('地址', width=100)

# 定义头

tree.heading('学号', text='学号')

tree.heading('姓名', text='姓名')

tree.heading('电话', text='电话')

tree.heading('地址', text='地址')

def func(event):

    pass

tree.bind("", func)  # 鼠标右键

tree.grid(row=6, column=1, columnspan=6, pady=5, sticky='e')

def Judge(stuid):  # 判断stuid(str型)是否存在库中

    flag = 0  # 判断标志,1为stuid存在数据库中

    lst = database.showAllData()

    for i in range(len(lst)):

        for item in lst[i]:

            if int(stuid) == item:

                flag = 1

    return flag

def ClearEntry():  # 清空控件内容

    entryid.delete(0, 'end')

    entryna.delete(0, 'end')

    entryph.delete(0, 'end')

    entryadr.delete(0, 'end')

def ClearTree():  # 清空树控件

    x = tree.get_children()

    for item in x:  # 清空控件内容

        tree.delete(item)

def addInformation():

    res = [entryid.get(), entryna.get(), entryph.get(), entryadr.get()]

    if (not entryid.get()) or (not entryna.get()) or (not entryph.get()) or (

            not entryadr.get()):

        tk.messagebox.showerror(title='Attention', message='输入信息不完整')

    elif (len(entryid.get()) != 8) or not (entryid.get()).isdecimal():

        tk.messagebox.showerror(title='Attention', message='输入错误,学号为8位数字')

    elif len(entryph.get()) != 11 or not (entryph.get()).isdecimal():

        tk.messagebox.showerror(title='Attention', message='输入错误,电话为11位数字')

    else:

        flag = Judge(entryid.get())  # 判断id是否已存在

        if not flag:

            database.addRec(int(entryid.get()), entryna.get(),

                            int(entryph.get()), entryadr.get())

            tk.messagebox.showinfo(title='Attention', message='添加成功')

            tree.insert('', 'end', values=res)

            ClearEntry()  # 清空所有输入框

        else:

            tk.messagebox.showinfo(title='Attention', message='学号已存在')

def deleteInformation():  # 删除

    temp = entryid.get()

    if not temp:

        tk.messagebox.showinfo(title='Attention', message='学生学号为空')

    elif not temp.isdecimal() or (len(entryid.get()) != 8):

        tk.messagebox.showerror(title='Attention', message='输入错误,学号为8位数字')

    else:

        flag = Judge(temp)

        if flag:  # 存在

            database.deleteRec(temp)

            tk.messagebox.showinfo(title='Attention', message='删除成功')

            showData()  # 刷新显示数据

            Clear()  # 清空所有输入框

        else:

            tk.messagebox.showinfo(title='Attention', message='学号不存在')

def showInformation():  # 显示全部信息

    ClearTree()

    lst = database.showAllData()

    for item in lst:

        tree.insert('', 'end', values=item)

def searchInformation():

    temp = entryid.get()

    if not temp:

        tk.messagebox.showinfo(title='Attention', message='学生学号为空')

    elif not temp.isdecimal() or (len(entryid.get()) != 8):

        tk.messagebox.showerror(title='Attention', message='输入错误,学号为8位数字')

    else:

        flag = Judge(temp)

        ClearTree()

        if flag:  # 存在

            res = database.searchRec(temp)

            tree.insert('', 'end', values=res)

        else:

            tk.messagebox.showinfo(title='Attention', message='学号不存在')

bt1 = tk.Button(win, text='显示所有信息', width=10,

                command=showInformation).grid(row=4, column=2, pady=10)

bt2 = tk.Button(win, text='追加信息', width=10,

                command=addInformation).grid(row=4, column=3, pady=10)

bt3 = tk.Button(win, text='删除信息', width=10,

                command=deleteInformation).grid(row=4, column=4, pady=10)

bt4 = tk.Button(win, text='搜索信息', width=10,

                command=searchInformation).grid(row=4, column=5, pady=10)

win.mainloop()

你可能感兴趣的:(【基础】练习册42-Python3_带数据库的学生管理系统)