今日代码:
复制代码
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from PIL import Image,ImageTk
import tkinter
import sqlite3



#创建本地数据库
#提交的文件中存在数据库,所以该数据库的创建程序可以不运行

"""


conn.execute ('''CREATE TABLE StudentTable(
   ID INTEGER PRIMARY KEY   AUTOINCREMENT,
   StuId         INTEGER     NOT NULL,
   NAME           TEXT      NOT NULL,
   CLASS            INT       NOT NULL,
   AGE           INTEGER     Not NULL);''')
print("Table created successfully");

#创建本地数据库




"""
#打开本地数据库用于存储用户信息
conn = sqlite3.connect('student1.db')
#主界面
root=Tk()
root.title("学生信息管理系统")
root.config(width=600)
root.config(height=600)

#添加窗口背景图片
canvas=tkinter.Canvas(root,
                      width=600,            #指定Canvas组件的宽度
                      height=600,           #指定Canvas组件的高度
                      bg='white'            #指定Canvas组件的背景色
                      #im=tkinter.PhotoImage(file='img.gif')    使用PhotoImage打开图片


                      )



"""               记得在运行时修改文件所在位置。*********************************************************                                                                     """


image=Image.open("学校.png")
im=ImageTk.PhotoImage(image)

canvas.create_image(400,200,image=im)    #使用creat_image将图片添加到Canvas
canvas.pack()



"""******************************************************************************************"""

"""************************       录入信息部分      ********************************************"""




#增加学生信息
def insert_stu():  #录入学生信息
    root1=Tk()
    root1.title("录入学生信息")
    root1.config(width=600)
    root1.config(height=600)






    #创建关联字符变量
    varName=StringVar(root1,value='')
    varId=StringVar(root1,value='')
    varClass=StringVar(root1,value='')
    varAge=StringVar(root1,value='')



    #创建标签组件
    label=Label(root1,text="姓名:",font=("微软雅黑 -20"))
    #label.grid(row=0,sticky=E)
    label.place(x=130,y=60,height=40,width=80)

    label=Label(root1,text="学号:",font=("微软雅黑 -20"))
    #label.grid(row=1,sticky=E)
    label.place(x=130,y=110,height=40,width=80)


    label=Label(root1,text="班级:",font=("微软雅黑 -20"))
    #label.grid(row=2,sticky=E)
    label.place(x=130,y=160,height=40,width=80)


    label=Label(root1,text="年龄:",font=("微软雅黑 -20"))
    #label.grid(row=3,sticky=E)
    label.place(x=130,y=210,height=40,width=80)

    #创建文本框组件,同时设置关联的变量
    #    姓名entryName
    #    学号entryId
    #    班级entryClass
    #    年龄entryAge


    entryName=Entry((root1),textvariable=varName)
    #entryName.grid(row=0,column=1,sticky=W)
    entryName.place(x=190,y=60,height=40,width=200)

    entryId=Entry((root1),textvariable=varId)
    #entryId.grid(row=1,column=1,sticky=W)
    entryId.place(x=190,y=110,height=40,width=200)

    entryClass=Entry((root1),textvariable=varClass)
    #entryClass.grid(row=2,column=1,sticky=W)
    entryClass.place(x=190,y=160,height=40,width=200)

    entryAge=Entry((root1),textvariable=varAge)
    #entryAge.grid(row=3,column=1,sticky=W)
    entryAge.place(x=190,y=210,height=40,width=200)


    def buttonOK():
        i=0

        conn = sqlite3.connect('student1.db')

        stu_id = eval(entryId.get())#学号输入
        stu_name =str(entryName.get())#姓名录入
        stu_class =eval(entryClass.get())#班级录入
        stu_age=eval(entryAge.get())#年龄录入

        cursor = conn.execute("SELECT * from StudentTable;")
        conn.commit()
        for row in cursor:#进行遍历查找是否有重复的学号
            if stu_id==row[0]:
                i=1
                break
            else:
                i=0
                #查找完成若有重复的学号,则警告。否则录入数据库
        if i==1:
            messagebox.showerror('警告',message='学号重复,请重新输入')
        else:
            try:
                sql1 = "INSERT INTO StudentTable(StuId,NAME,CLASS,AGE)"
                sql1+="VALUES(%d,'%s',%d,%d)"%(stu_id,stu_name,stu_class,stu_age)
                conn.execute(sql1)
                conn.commit()
                messagebox.showinfo(title='恭喜',message='录入成功!')
                root1.destroy()
            except:
                messagebox.showerror('警告',message='未录入成功')


    buttonbuttonOK=Button(root1,text="录入学生信息",font=("微软雅黑 -20"),command=buttonOK)
    buttonbuttonOK.place(x=170,y=300,height=40,width=200)
    def cancel():
        varName.set('')
        varId.set('')
        varClass.set('')
        varAge.set('')


    #取消键
    buttonCancel=Button(root1,text="取消",font=("微软雅黑 -20"),command=cancel)
    buttonCancel.place(x=170,y=350,height=40,width=200)

    #退出键
    buttondel=Button(root1,text="退出",font=("微软雅黑 -20"),command=root1.destroy)
    buttondel.place(x=170,y=400,height=40,width=200)
    root1.mainloop()



#录入完成
"""******************************************************************************************"""
posted on 2024-06-19 19:01    阅读(11)  评论(0)    收藏  举报