python編寫學生信息管理系統的增刪改查功能


首先需要創建數據庫

我這里是創建本地數據庫

代碼為

1 conn.execute ('''CREATE TABLE IF EXISTS StudentTable(
2    ID INTEGER PRIMARY KEY   AUTOINCREMENT,
3    StuId         INTEGER     NOT NULL,
4    NAME           TEXT      NOT NULL,
5    CLASS            INT       NOT NULL,
6    AGE           INTEGER     Not NULL);''')
7 print("Table created successfully");

對主頁的設計,添加本地圖片

 1 #打開本地數據庫用於存儲用戶信息
 2 conn = sqlite3.connect('student1.db')
 3 #主界面
 4 root=Tk()
 5 root.title("學生信息管理系統")
 6 root.config(width=600)
 7 root.config(height=600)
 8 
 9 #添加窗口背景圖片
10 canvas=tkinter.Canvas(root,
11                       width=600,            #指定Canvas組件的寬度
12                       height=600,           #指定Canvas組件的高度
13                       bg='white'            #指定Canvas組件的背景色
14                       #im=tkinter.PhotoImage(file='img.gif')    使用PhotoImage打開圖片
15 
16 
17                       )
18 
19 
20 
21 """               記得在運行時修改文件所在位置。*********************************************************                                                                     """
22 
23 
24 image=Image.open("E:\pathon\實驗\大作業\愛心.jpg")
25 im=ImageTk.PhotoImage(image)
26 
27 canvas.create_image(400,200,image=im)    #使用creat_image將圖片添加到Canvas
28 canvas.pack()

主頁

 1 #創建頂級菜單及其下拉菜單
 2 menubar=Menu(root)
 3 filemenu=Menu(menubar,tearoff=False)
 4 filemenu.add_command(label="增加",command=insert_stu)  
 5 filemenu.add_command(label="刪除",command=delete_stu)#command接刪除函數/下面接修改函數
 6 filemenu.add_command(label="修改",command=change_stu)
 7 filemenu.add_command(label="查詢",command=sel_stu)
 8 
 9 filemenu.add_separator()
10 filemenu.add_command(label="退出",command=root.destroy)
11 menubar.add_cascade(label="菜單",menu=filemenu)  
12     
13 #顯示菜單   
14 root.config(menu=menubar)
15 
16 buttoninsert_stu=Button(root,text="錄入學生信息",font=("微軟雅黑 -20"),command=insert_stu)
17 #buttoninsert_stu.grid(row=2,column=0)由下面的代碼將該代碼覆蓋,顯示的是在界面上的位置
18 buttoninsert_stu.place(x=50,y=50,height=40,width=200)
19 
20 buttondelete_stu=Button(root,text="刪除學生信息",font=("微軟雅黑 -20"),command=delete_stu)
21 #buttondelete_stu.grid(row=2,column=1)
22 buttondelete_stu.place(x=50,y=150,height=40,width=200)
23 
24 buttonchange_stu=Button(root,text="修改學生信息",font=("微軟雅黑 -20"),command=change_stu)
25 #buttonchange_stu.grid(row=4,column=0)
26 buttonchange_stu.place(x=50,y=250,height=40,width=200)
27 
28 buttonsel_stu=Button(root,text="查詢學生信息",font=("微軟雅黑 -20"),command=sel_stu)
29 #buttonsel_stu.grid(row=4,column=1)
30 buttonsel_stu.place(x=50,y=350,height=40,width=200)

分批次操作

實現增加學生信息的功能

  1 #增加學生信息
  2 def insert_stu():  #錄入學生信息
  3     root1=Tk()
  4     root1.title("錄入學生信息")
  5     root1.config(width=600)
  6     root1.config(height=600)   
  7     
  8    
  9 
 10     
 11     
 12     
 13     #創建關聯字符變量
 14     varName=StringVar(root1,value='')
 15     varId=StringVar(root1,value='')
 16     varClass=StringVar(root1,value='')
 17     varAge=StringVar(root1,value='') 
 18 
 19 
 20     
 21     #創建標簽組件
 22     label=Label(root1,text="姓名:",font=("微軟雅黑 -20"))
 23     #label.grid(row=0,sticky=E)
 24     label.place(x=30,y=60,height=40,width=80)
 25     
 26     label=Label(root1,text="學號:",font=("微軟雅黑 -20"))
 27     #label.grid(row=1,sticky=E)
 28     label.place(x=30,y=110,height=40,width=80)
 29     
 30 
 31     label=Label(root1,text="班級:",font=("微軟雅黑 -20"))
 32     #label.grid(row=2,sticky=E)
 33     label.place(x=30,y=160,height=40,width=80)
 34     
 35     
 36     label=Label(root1,text="年齡:",font=("微軟雅黑 -20"))
 37     #label.grid(row=3,sticky=E)
 38     label.place(x=30,y=210,height=40,width=80)
 39     
 40     #創建文本框組件,同時設置關聯的變量
 41     #    姓名entryName
 42     #    學號entryId
 43     #    班級entryClass
 44     #    年齡entryAge
 45 
 46     
 47     entryName=Entry((root1),textvariable=varName)
 48     #entryName.grid(row=0,column=1,sticky=W)
 49     entryName.place(x=120,y=60,height=40,width=200)
 50     
 51     entryId=Entry((root1),textvariable=varId)
 52     #entryId.grid(row=1,column=1,sticky=W)
 53     entryId.place(x=120,y=110,height=40,width=200)
 54     
 55     entryClass=Entry((root1),textvariable=varClass)
 56     #entryClass.grid(row=2,column=1,sticky=W)
 57     entryClass.place(x=120,y=160,height=40,width=200)
 58     
 59     entryAge=Entry((root1),textvariable=varAge)
 60     #entryAge.grid(row=3,column=1,sticky=W)
 61     entryAge.place(x=120,y=210,height=40,width=200)
 62     
 63     
 64     def buttonOK():
 65         i=0
 66 
 67         conn = sqlite3.connect('student1.db')
 68 
 69         stu_id = eval(entryId.get())#學號輸入
 70         stu_name =str(entryName.get())#姓名錄入
 71         stu_class =eval(entryClass.get())#班級錄入
 72         stu_age=eval(entryAge.get())#年齡錄入  
 73         
 74         cursor = conn.execute("SELECT * from StudentTable;")
 75         conn.commit()
 76         for row in cursor:#進行遍歷查找是否有重復的學號
 77             if stu_id==row[0]:
 78                 i=1
 79                 break
 80             else:
 81                 i=0
 82                 #查找完成若有重復的學號,則警告。否則錄入數據庫
 83         if i==1:
 84             messagebox.showerror('警告',message='學號重復,請重新輸入')
 85         else:
 86             try:
 87                 sql1 = "INSERT INTO StudentTable(StuId,NAME,CLA,AGE)"
 88                 sql1+="VALUES(%d,'%s',%d,%d)"%(stu_id,stu_name,stu_class,stu_age)
 89                 conn.execute(sql1)                               
 90                 conn.commit()
 91                 messagebox.showinfo(title='恭喜',message='錄入成功!')
 92                 root1.destroy()
 93             except:
 94                 messagebox.showerror('警告',message='未錄入成功')
 95 
 96            
 97     buttonbuttonOK=Button(root1,text="錄入學生信息",font=("微軟雅黑 -20"),command=buttonOK)
 98     buttonbuttonOK.place(x=150,y=300,height=40,width=200)
 99     def cancel():
100         varName.set('')
101         varId.set('')
102         varClass.set('')
103         varAge.set('')
104 
105         
106     #取消鍵
107     buttonCancel=Button(root1,text="取消",font=("微軟雅黑 -20"),command=cancel)
108     buttonCancel.place(x=150,y=350,height=40,width=200)
109     
110     #退出鍵
111     buttondel=Button(root1,text="退出",font=("微軟雅黑 -20"),command=root1.destroy)
112     buttondel.place(x=150,y=400,height=40,width=200)
113     root1.mainloop()

實現刪除學生信息功能

 1 def delete_stu(): 
 2     root2=Tk()
 3     root2.title("刪除學生信息")
 4     root2.config(width=600)
 5     root2.config(height=600)
 6     
 7     
 8     #添加窗口背景圖片
 9     
10     
11     
12     #創建標簽
13     label=Label(root2,text="學號:",font=("微軟雅黑 -20"))
14     #label.grid(row=1,sticky=E)
15     label.place(x=30,y=20,height=40,width=80)
16     
17     entryId=Entry(root2)
18     entryId.place(x=120,y=20,height=40,width=200)
19     
20     def delete():
21 
22         conn = sqlite3.connect('student1.db')
23         stu_id = eval(entryId.get())#學號輸入
24         conn.execute("DELETE from StudentTable where StuId = '%s';"%stu_id)
25         conn.commit()
26         messagebox.showinfo(title='恭喜',message='刪除成功!')
27         root2.destroy()
28         
29         
30         
31         
32         
33     #刪除鍵   
34     buttondelete=Button(root2,text="刪除",font=("微軟雅黑 -20"),command=delete)
35     buttondelete.place(x=150,y=160,height=40,width=200)
36     
37     #退出鍵
38     buttondel=Button(root2,text="退出",font=("微軟雅黑 -20"),command=root2.destroy)
39     buttondel.place(x=150,y=210,height=40,width=200)
40 
41 
42     root2.mainloop()

實現查詢功能

  1 def sel_stu():
  2     root3=Tk()
  3     root3.title("查詢學生信息")
  4     root3.config(width=600)
  5     root3.config(height=600) 
  6     
  7     
  8    
  9     
 10     #創建關聯變量
 11     sId=StringVar(root3,value='')
 12     
 13     #創建文本組件框\標簽組件
 14     label=Label(root3,text="學號",font=("微軟雅黑 -20"))
 15     label.place(x=30,y=10,height=40,width=80)
 16     
 17     selId=Entry((root3),textvariable=sId)
 18     selId.place(x=120,y=10,height=40,width=200)
 19     
 20 
 21     
 22     def select():
 23         
 24         #創建關聯字符變量
 25         varName=StringVar(root3,value='')
 26         varId=StringVar(root3,value='')
 27         varClass=StringVar(root3,value='')
 28         varAge=StringVar(root3,value='')         
 29         
 30         
 31         conn = sqlite3.connect('student1.db')
 32         stu_id = eval(selId.get())#學號輸入
 33         cursor = conn.execute("SELECT * from StudentTable where StuId = '%d';"%stu_id)
 34         conn.commit()
 35         for row in cursor:
 36              if stu_id == row[0]:
 37                  stu_name=row[1]
 38                  stu_class=row[2]
 39                  stu_age=row[3]
 40 
 41 
 42 
 43 
 44         #創建標簽組件
 45         label=Label(root3,text="姓名:",font=("微軟雅黑 -20"))
 46         #label.grid(row=0,sticky=E)
 47         label.place(x=30,y=110,height=40,width=80)
 48     
 49         label=Label(root3,text="學號:",font=("微軟雅黑 -20"))
 50         #label.grid(row=1,sticky=E)
 51         label.place(x=30,y=160,height=40,width=80)
 52     
 53 
 54         label=Label(root3,text="班級:",font=("微軟雅黑 -20"))
 55         #label.grid(row=2,sticky=E)
 56         label.place(x=30,y=210,height=40,width=80)
 57     
 58     
 59         label=Label(root3,text="年齡:",font=("微軟雅黑 -20"))
 60         #label.grid(row=3,sticky=E)
 61         label.place(x=30,y=260,height=40,width=80)
 62     
 63         #創建文本框組件,同時設置關聯的變量
 64         #    姓名entryName
 65         #    學號entryId
 66         #    班級entryClass
 67         #    年齡entryAge
 68     
 69         entryName=Entry((root3),textvariable=varName)
 70         #entryName.grid(row=0,column=1,sticky=W)
 71         entryName.place(x=120,y=110,height=40,width=200)
 72     
 73         entryId=Entry((root3),textvariable=varId)
 74         #entryId.grid(row=1,column=1,sticky=W)
 75         entryId.place(x=120,y=160,height=40,width=200)
 76      
 77         entryClass=Entry((root3),textvariable=varClass)
 78         #entryClass.grid(row=2,column=1,sticky=W)
 79         entryClass.place(x=120,y=210,height=40,width=200)
 80     
 81         entryAge=Entry((root3),textvariable=varAge)
 82         #entryAge.grid(row=3,column=1,sticky=W)
 83         entryAge.place(x=120,y=260,height=40,width=200)
 84         
 85         varName.set(stu_name)
 86         varId.set(stu_id)
 87         varClass.set(stu_class)
 88         varAge.set(stu_age)
 89     
 90     #查詢鍵
 91     buttonselect=Button(root3,text="查詢",font=("微軟雅黑 -20"),command=select)
 92     buttonselect.place(x=200,y=60,height=40,width=100) 
 93     
 94     #取消鍵
 95     def cancel():
 96         sId.set('')
 97     
 98     buttoncancel=Button(root3,text="取消",font="微軟雅黑 -20",command=cancel)
 99     buttoncancel.place(x=50,y=60,height=40,width=100)
100     
101     #退出鍵
102     buttondel=Button(root3,text="退出",font="微軟雅黑 -20",command=root3.destroy)
103     buttondel.place(x=350,y=60,height=40,width=100)
104     root3.mainloop()

修改功能

  1 def change_stu():
  2     root4=Tk()
  3     root4.title("修改學生信息")
  4     root4.config(width=600)
  5     root4.config(height=600) 
  6     
  7     #創建關聯變量
  8     sId=StringVar(root4,value='')
  9     
 10     #創建文本組件框\標簽組件
 11     label=Label(root4,text="學號",font=("微軟雅黑 -20"))
 12     label.place(x=30,y=10,height=40,width=80)
 13     
 14     selId=Entry((root4),textvariable=sId)
 15     selId.place(x=120,y=10,height=40,width=200)
 16     
 17     #創建關聯字符變量
 18     varName=StringVar(root4,value='')
 19     varId=StringVar(root4,value='')
 20     varClass=StringVar(root4,value='')
 21     varAge=StringVar(root4,value='')
 22     
 23     #創建標簽組件
 24     
 25     label=Label(root4,text="姓名:",font=("微軟雅黑 -20"))
 26     #label.grid(row=0,sticky=E)
 27     label.place(x=30,y=110,height=40,width=80)
 28     
 29     label=Label(root4,text="學號:",font=("微軟雅黑 -20"))
 30     #label.grid(row=1,sticky=E)
 31     label.place(x=30,y=160,height=40,width=80)
 32     
 33 
 34     label=Label(root4,text="班級:",font=("微軟雅黑 -20"))
 35     #label.grid(row=2,sticky=E)
 36     label.place(x=30,y=210,height=40,width=80)
 37     
 38     
 39     label=Label(root4,text="年齡:",font=("微軟雅黑 -20"))
 40     #label.grid(row=3,sticky=E)
 41     label.place(x=30,y=260,height=40,width=80)
 42     
 43         #創建文本框組件,同時設置關聯的變量
 44         #    姓名entryName
 45         #    學號entryId
 46         #    班級entryClass
 47         #    年齡entryAge
 48     entryName=Entry((root4),textvariable=varName)
 49         #entryName.grid(row=0,column=1,sticky=W)
 50     entryName.place(x=120,y=110,height=40,width=200)
 51     
 52     entryId=Entry((root4),textvariable=varId)
 53         #entryId.grid(row=1,column=1,sticky=W)
 54     entryId.place(x=120,y=160,height=40,width=200)
 55      
 56     entryClass=Entry((root4),textvariable=varClass)
 57         #entryClass.grid(row=2,column=1,sticky=W)
 58     entryClass.place(x=120,y=210,height=40,width=200)
 59     
 60     entryAge=Entry((root4),textvariable=varAge)
 61         #entryAge.grid(row=3,column=1,sticky=W)
 62     entryAge.place(x=120,y=260,height=40,width=200)
 63       
 64     def select():
 65    
 66         conn = sqlite3.connect('student1.db')
 67         stu_id = eval(selId.get())#學號輸入
 68         cursor = conn.execute("SELECT * from StudentTable where StuId = %d;"%stu_id)
 69         conn.commit()
 70         for row in cursor:
 71              if stu_id == row[0]:
 72                  stu_name=row[1]
 73                  stu_class=row[2]
 74                  stu_age=row[3]
 75  
 76         varName.set(stu_name)
 77         varId.set(stu_id)
 78         varClass.set(stu_class)
 79         varAge.set(stu_age)
 80       
 81     def saveName():
 82         name=entryName.get()
 83         conn=sqlite3.connect('student1.db')
 84         sql="UPDATE StudentTable SET NAME='%s' WHERE StuId=%d;"%(name,eval(selId.get()))
 85         conn.execute(sql)
 86         conn.commit()  
 87 
 88         messagebox.showinfo(title='恭喜',message='保存成功!')
 89     
 90     def saveCla():
 91         cla=eval(entryClass.get())
 92         conn=sqlite3.connect('student1.db')
 93         sql="UPDATE StudentTable SET CLA=%d WHERE StuId=%d;"%(cla,eval(selId.get()))
 94         conn.execute(sql)
 95         conn.commit()
 96 
 97         messagebox.showinfo(title='恭喜',message='保存成功!')
 98 
 99     def saveAge():
100         age=eval(entryAge.get())
101         conn=sqlite3.connect('student1.db')
102         sql="UPDATE StudentTable SET AGE=%d WHERE StuId=%d;"%(age,eval(selId.get()))
103         conn.execute(sql)
104         conn.commit()
105 
106         messagebox.showinfo(title='恭喜',message='保存成功!') 
107     
108     
109     #保存鍵
110     buttonname=Button(root4,text="保存",font=("微軟雅黑 -20"),command=saveName)
111     buttonname.place(x=330,y=110,height=40,width=60)
112     
113     buttoncla=Button(root4,text="保存",font=("微軟雅黑 -20"),command=saveCla)
114     buttoncla.place(x=330,y=210,height=40,width=60)
115     
116     buttonage=Button(root4,text="保存",font=("微軟雅黑 -20"),command=saveAge)
117     buttonage.place(x=330,y=260,height=40,width=60)
118 
119     def cancel():
120         sId.set('')        
121 
122     #取消鍵    
123     buttoncancel=Button(root4,text="取消",font=("微軟雅黑 -20"),command=cancel)
124     buttoncancel.place(x=20,y=60,height=40,width=60)
125     
126     #查詢鍵
127     buttonselect=Button(root4,text="查詢",font=("微軟雅黑 -20"),command=select)
128     buttonselect.place(x=100,y=60,height=40,width=60)    
129    
130     #退出鍵
131     buttondel=Button(root4,text="退出",font="微軟雅黑 -20",command=root4.destroy)
132     buttondel.place(x=260,y=60,height=40,width=60)
133    
134     root4.mainloop()

完整代碼

  1 from tkinter import *
  2 from tkinter import messagebox
  3 from tkinter import filedialog
  4 from PIL import Image,ImageTk
  5 import tkinter
  6 import sqlite3
  7 
  8 
  9 
 10 #創建本地數據庫
 11 #提交的文件中存在數據庫,所以該數據庫的創建程序可以不運行
 12 
 13 """
 14 
 15 
 16 conn.execute ('''CREATE TABLE StudentTable(
 17    ID INTEGER PRIMARY KEY   AUTOINCREMENT,
 18    StuId         INTEGER     NOT NULL,
 19    NAME           TEXT      NOT NULL,
 20    CLASS            INT       NOT NULL,
 21    AGE           INTEGER     Not NULL);''')
 22 print("Table created successfully");
 23 
 24 #創建本地數據庫
 25 
 26 
 27 
 28 
 29 """
 30 #打開本地數據庫用於存儲用戶信息
 31 conn = sqlite3.connect('student1.db')
 32 #主界面
 33 root=Tk()
 34 root.title("學生信息管理系統")
 35 root.config(width=600)
 36 root.config(height=600)
 37 
 38 #添加窗口背景圖片
 39 canvas=tkinter.Canvas(root,
 40                       width=600,            #指定Canvas組件的寬度
 41                       height=600,           #指定Canvas組件的高度
 42                       bg='white'            #指定Canvas組件的背景色
 43                       #im=tkinter.PhotoImage(file='img.gif')    使用PhotoImage打開圖片
 44 
 45 
 46                       )
 47 
 48 
 49 
 50 """               記得在運行時修改文件所在位置。*********************************************************                                                                     """
 51 
 52 
 53 image=Image.open("E:\愛心.jpg")
 54 im=ImageTk.PhotoImage(image)
 55 
 56 canvas.create_image(400,200,image=im)    #使用creat_image將圖片添加到Canvas
 57 canvas.pack()
 58 
 59 
 60 
 61 """******************************************************************************************"""
 62 
 63 """************************       錄入信息部分      ********************************************"""
 64 
 65 
 66 
 67 
 68 #增加學生信息
 69 def insert_stu():  #錄入學生信息
 70     root1=Tk()
 71     root1.title("錄入學生信息")
 72     root1.config(width=600)
 73     root1.config(height=600)   
 74     
 75    
 76 
 77     
 78     
 79     
 80     #創建關聯字符變量
 81     varName=StringVar(root1,value='')
 82     varId=StringVar(root1,value='')
 83     varClass=StringVar(root1,value='')
 84     varAge=StringVar(root1,value='') 
 85 
 86 
 87     
 88     #創建標簽組件
 89     label=Label(root1,text="姓名:",font=("微軟雅黑 -20"))
 90     #label.grid(row=0,sticky=E)
 91     label.place(x=30,y=60,height=40,width=80)
 92     
 93     label=Label(root1,text="學號:",font=("微軟雅黑 -20"))
 94     #label.grid(row=1,sticky=E)
 95     label.place(x=30,y=110,height=40,width=80)
 96     
 97 
 98     label=Label(root1,text="班級:",font=("微軟雅黑 -20"))
 99     #label.grid(row=2,sticky=E)
100     label.place(x=30,y=160,height=40,width=80)
101     
102     
103     label=Label(root1,text="年齡:",font=("微軟雅黑 -20"))
104     #label.grid(row=3,sticky=E)
105     label.place(x=30,y=210,height=40,width=80)
106     
107     #創建文本框組件,同時設置關聯的變量
108     #    姓名entryName
109     #    學號entryId
110     #    班級entryClass
111     #    年齡entryAge
112 
113     
114     entryName=Entry((root1),textvariable=varName)
115     #entryName.grid(row=0,column=1,sticky=W)
116     entryName.place(x=120,y=60,height=40,width=200)
117     
118     entryId=Entry((root1),textvariable=varId)
119     #entryId.grid(row=1,column=1,sticky=W)
120     entryId.place(x=120,y=110,height=40,width=200)
121     
122     entryClass=Entry((root1),textvariable=varClass)
123     #entryClass.grid(row=2,column=1,sticky=W)
124     entryClass.place(x=120,y=160,height=40,width=200)
125     
126     entryAge=Entry((root1),textvariable=varAge)
127     #entryAge.grid(row=3,column=1,sticky=W)
128     entryAge.place(x=120,y=210,height=40,width=200)
129     
130     
131     def buttonOK():
132         i=0
133 
134         conn = sqlite3.connect('student1.db')
135 
136         stu_id = eval(entryId.get())#學號輸入
137         stu_name =str(entryName.get())#姓名錄入
138         stu_class =eval(entryClass.get())#班級錄入
139         stu_age=eval(entryAge.get())#年齡錄入  
140         
141         cursor = conn.execute("SELECT * from StudentTable;")
142         conn.commit()
143         for row in cursor:#進行遍歷查找是否有重復的學號
144             if stu_id==row[0]:
145                 i=1
146                 break
147             else:
148                 i=0
149                 #查找完成若有重復的學號,則警告。否則錄入數據庫
150         if i==1:
151             messagebox.showerror('警告',message='學號重復,請重新輸入')
152         else:
153             try:
154                 sql1 = "INSERT INTO StudentTable(StuId,NAME,CLA,AGE)"
155                 sql1+="VALUES(%d,'%s',%d,%d)"%(stu_id,stu_name,stu_class,stu_age)
156                 conn.execute(sql1)                               
157                 conn.commit()
158                 messagebox.showinfo(title='恭喜',message='錄入成功!')
159                 root1.destroy()
160             except:
161                 messagebox.showerror('警告',message='未錄入成功')
162 
163            
164     buttonbuttonOK=Button(root1,text="錄入學生信息",font=("微軟雅黑 -20"),command=buttonOK)
165     buttonbuttonOK.place(x=150,y=300,height=40,width=200)
166     def cancel():
167         varName.set('')
168         varId.set('')
169         varClass.set('')
170         varAge.set('')
171 
172         
173     #取消鍵
174     buttonCancel=Button(root1,text="取消",font=("微軟雅黑 -20"),command=cancel)
175     buttonCancel.place(x=150,y=350,height=40,width=200)
176     
177     #退出鍵
178     buttondel=Button(root1,text="退出",font=("微軟雅黑 -20"),command=root1.destroy)
179     buttondel.place(x=150,y=400,height=40,width=200)
180     root1.mainloop()
181 
182 
183 
184 #錄入完成
185 """******************************************************************************************"""
186 
187 """************************       刪除的部分      ********************************************"""
188 #刪除學生信息
189 
190 
191  
192 def delete_stu(): 
193     root2=Tk()
194     root2.title("刪除學生信息")
195     root2.config(width=600)
196     root2.config(height=600)
197     
198     
199     #添加窗口背景圖片
200     
201     
202     
203     #創建標簽
204     label=Label(root2,text="學號:",font=("微軟雅黑 -20"))
205     #label.grid(row=1,sticky=E)
206     label.place(x=30,y=20,height=40,width=80)
207     
208     entryId=Entry(root2)
209     entryId.place(x=120,y=20,height=40,width=200)
210     
211     def delete():
212 
213         conn = sqlite3.connect('student1.db')
214         stu_id = eval(entryId.get())#學號輸入
215         conn.execute("DELETE from StudentTable where StuId = '%s';"%stu_id)
216         conn.commit()
217         messagebox.showinfo(title='恭喜',message='刪除成功!')
218         root2.destroy()
219         
220         
221         
222         
223         
224     #刪除鍵   
225     buttondelete=Button(root2,text="刪除",font=("微軟雅黑 -20"),command=delete)
226     buttondelete.place(x=150,y=160,height=40,width=200)
227     
228     #退出鍵
229     buttondel=Button(root2,text="退出",font=("微軟雅黑 -20"),command=root2.destroy)
230     buttondel.place(x=150,y=210,height=40,width=200)
231 
232 
233     root2.mainloop()
234 #刪除完成
235 """******************************************************************************************"""
236 
237 """************************       查詢的部分      ********************************************"""
238 
239 
240 
241 #查詢學生信息
242 def sel_stu():
243     root3=Tk()
244     root3.title("查詢學生信息")
245     root3.config(width=600)
246     root3.config(height=600) 
247     
248     
249    
250     
251     #創建關聯變量
252     sId=StringVar(root3,value='')
253     
254     #創建文本組件框\標簽組件
255     label=Label(root3,text="學號",font=("微軟雅黑 -20"))
256     label.place(x=30,y=10,height=40,width=80)
257     
258     selId=Entry((root3),textvariable=sId)
259     selId.place(x=120,y=10,height=40,width=200)
260     
261 
262     
263     def select():
264         
265         #創建關聯字符變量
266         varName=StringVar(root3,value='')
267         varId=StringVar(root3,value='')
268         varClass=StringVar(root3,value='')
269         varAge=StringVar(root3,value='')         
270         
271         
272         conn = sqlite3.connect('student1.db')
273         stu_id = eval(selId.get())#學號輸入
274         cursor = conn.execute("SELECT * from StudentTable where StuId = '%d';"%stu_id)
275         conn.commit()
276         for row in cursor:
277              if stu_id == row[0]:
278                  stu_name=row[1]
279                  stu_class=row[2]
280                  stu_age=row[3]
281 
282 
283 
284 
285         #創建標簽組件
286         label=Label(root3,text="姓名:",font=("微軟雅黑 -20"))
287         #label.grid(row=0,sticky=E)
288         label.place(x=30,y=110,height=40,width=80)
289     
290         label=Label(root3,text="學號:",font=("微軟雅黑 -20"))
291         #label.grid(row=1,sticky=E)
292         label.place(x=30,y=160,height=40,width=80)
293     
294 
295         label=Label(root3,text="班級:",font=("微軟雅黑 -20"))
296         #label.grid(row=2,sticky=E)
297         label.place(x=30,y=210,height=40,width=80)
298     
299     
300         label=Label(root3,text="年齡:",font=("微軟雅黑 -20"))
301         #label.grid(row=3,sticky=E)
302         label.place(x=30,y=260,height=40,width=80)
303     
304         #創建文本框組件,同時設置關聯的變量
305         #    姓名entryName
306         #    學號entryId
307         #    班級entryClass
308         #    年齡entryAge
309     
310         entryName=Entry((root3),textvariable=varName)
311         #entryName.grid(row=0,column=1,sticky=W)
312         entryName.place(x=120,y=110,height=40,width=200)
313     
314         entryId=Entry((root3),textvariable=varId)
315         #entryId.grid(row=1,column=1,sticky=W)
316         entryId.place(x=120,y=160,height=40,width=200)
317      
318         entryClass=Entry((root3),textvariable=varClass)
319         #entryClass.grid(row=2,column=1,sticky=W)
320         entryClass.place(x=120,y=210,height=40,width=200)
321     
322         entryAge=Entry((root3),textvariable=varAge)
323         #entryAge.grid(row=3,column=1,sticky=W)
324         entryAge.place(x=120,y=260,height=40,width=200)
325         
326         varName.set(stu_name)
327         varId.set(stu_id)
328         varClass.set(stu_class)
329         varAge.set(stu_age)
330     
331     #查詢鍵
332     buttonselect=Button(root3,text="查詢",font=("微軟雅黑 -20"),command=select)
333     buttonselect.place(x=200,y=60,height=40,width=100) 
334     
335     #取消鍵
336     def cancel():
337         sId.set('')
338     
339     buttoncancel=Button(root3,text="取消",font="微軟雅黑 -20",command=cancel)
340     buttoncancel.place(x=50,y=60,height=40,width=100)
341     
342     #退出鍵
343     buttondel=Button(root3,text="退出",font="微軟雅黑 -20",command=root3.destroy)
344     buttondel.place(x=350,y=60,height=40,width=100)
345     root3.mainloop()
346 
347 
348 #查詢完成
349 """******************************************************************************************"""
350 
351 """************************       修改的部分      ********************************************"""
352 
353 
354 #修改學生信息
355 def change_stu():
356     root4=Tk()
357     root4.title("修改學生信息")
358     root4.config(width=600)
359     root4.config(height=600) 
360     
361     #創建關聯變量
362     sId=StringVar(root4,value='')
363     
364     #創建文本組件框\標簽組件
365     label=Label(root4,text="學號",font=("微軟雅黑 -20"))
366     label.place(x=30,y=10,height=40,width=80)
367     
368     selId=Entry((root4),textvariable=sId)
369     selId.place(x=120,y=10,height=40,width=200)
370     
371     #創建關聯字符變量
372     varName=StringVar(root4,value='')
373     varId=StringVar(root4,value='')
374     varClass=StringVar(root4,value='')
375     varAge=StringVar(root4,value='')
376     
377     #創建標簽組件
378     
379     label=Label(root4,text="姓名:",font=("微軟雅黑 -20"))
380     #label.grid(row=0,sticky=E)
381     label.place(x=30,y=110,height=40,width=80)
382     
383     label=Label(root4,text="學號:",font=("微軟雅黑 -20"))
384     #label.grid(row=1,sticky=E)
385     label.place(x=30,y=160,height=40,width=80)
386     
387 
388     label=Label(root4,text="班級:",font=("微軟雅黑 -20"))
389     #label.grid(row=2,sticky=E)
390     label.place(x=30,y=210,height=40,width=80)
391     
392     
393     label=Label(root4,text="年齡:",font=("微軟雅黑 -20"))
394     #label.grid(row=3,sticky=E)
395     label.place(x=30,y=260,height=40,width=80)
396     
397         #創建文本框組件,同時設置關聯的變量
398         #    姓名entryName
399         #    學號entryId
400         #    班級entryClass
401         #    年齡entryAge
402     entryName=Entry((root4),textvariable=varName)
403         #entryName.grid(row=0,column=1,sticky=W)
404     entryName.place(x=120,y=110,height=40,width=200)
405     
406     entryId=Entry((root4),textvariable=varId)
407         #entryId.grid(row=1,column=1,sticky=W)
408     entryId.place(x=120,y=160,height=40,width=200)
409      
410     entryClass=Entry((root4),textvariable=varClass)
411         #entryClass.grid(row=2,column=1,sticky=W)
412     entryClass.place(x=120,y=210,height=40,width=200)
413     
414     entryAge=Entry((root4),textvariable=varAge)
415         #entryAge.grid(row=3,column=1,sticky=W)
416     entryAge.place(x=120,y=260,height=40,width=200)
417       
418     def select():
419    
420         conn = sqlite3.connect('student1.db')
421         stu_id = eval(selId.get())#學號輸入
422         cursor = conn.execute("SELECT * from StudentTable where StuId = %d;"%stu_id)
423         conn.commit()
424         for row in cursor:
425              if stu_id == row[0]:
426                  stu_name=row[1]
427                  stu_class=row[2]
428                  stu_age=row[3]
429  
430         varName.set(stu_name)
431         varId.set(stu_id)
432         varClass.set(stu_class)
433         varAge.set(stu_age)
434       
435     def saveName():
436         name=entryName.get()
437         conn=sqlite3.connect('student1.db')
438         sql="UPDATE StudentTable SET NAME='%s' WHERE StuId=%d;"%(name,eval(selId.get()))
439         conn.execute(sql)
440         conn.commit()  
441 
442         messagebox.showinfo(title='恭喜',message='保存成功!')
443     
444     def saveCla():
445         cla=eval(entryClass.get())
446         conn=sqlite3.connect('student1.db')
447         sql="UPDATE StudentTable SET CLA=%d WHERE StuId=%d;"%(cla,eval(selId.get()))
448         conn.execute(sql)
449         conn.commit()
450 
451         messagebox.showinfo(title='恭喜',message='保存成功!')
452 
453     def saveAge():
454         age=eval(entryAge.get())
455         conn=sqlite3.connect('student1.db')
456         sql="UPDATE StudentTable SET AGE=%d WHERE StuId=%d;"%(age,eval(selId.get()))
457         conn.execute(sql)
458         conn.commit()
459 
460         messagebox.showinfo(title='恭喜',message='保存成功!') 
461     
462     
463     #保存鍵
464     buttonname=Button(root4,text="保存",font=("微軟雅黑 -20"),command=saveName)
465     buttonname.place(x=330,y=110,height=40,width=60)
466     
467     buttoncla=Button(root4,text="保存",font=("微軟雅黑 -20"),command=saveCla)
468     buttoncla.place(x=330,y=210,height=40,width=60)
469     
470     buttonage=Button(root4,text="保存",font=("微軟雅黑 -20"),command=saveAge)
471     buttonage.place(x=330,y=260,height=40,width=60)
472 
473     def cancel():
474         sId.set('')        
475 
476     #取消鍵    
477     buttoncancel=Button(root4,text="取消",font=("微軟雅黑 -20"),command=cancel)
478     buttoncancel.place(x=20,y=60,height=40,width=60)
479     
480     #查詢鍵
481     buttonselect=Button(root4,text="查詢",font=("微軟雅黑 -20"),command=select)
482     buttonselect.place(x=100,y=60,height=40,width=60)    
483    
484     #退出鍵
485     buttondel=Button(root4,text="退出",font="微軟雅黑 -20",command=root4.destroy)
486     buttondel.place(x=260,y=60,height=40,width=60)
487    
488     root4.mainloop()
489  
490 #創建頂級菜單及其下拉菜單
491 menubar=Menu(root)
492 filemenu=Menu(menubar,tearoff=False)
493 filemenu.add_command(label="增加",command=insert_stu)  
494 filemenu.add_command(label="刪除",command=delete_stu)#command接刪除函數/下面接修改函數
495 filemenu.add_command(label="修改",command=change_stu)
496 filemenu.add_command(label="查詢",command=sel_stu)
497 
498 filemenu.add_separator()
499 filemenu.add_command(label="退出",command=root.destroy)
500 menubar.add_cascade(label="菜單",menu=filemenu)  
501     
502 #顯示菜單   
503 root.config(menu=menubar)
504 
505 buttoninsert_stu=Button(root,text="錄入學生信息",font=("微軟雅黑 -20"),command=insert_stu)
506 #buttoninsert_stu.grid(row=2,column=0)由下面的代碼將該代碼覆蓋,顯示的是在界面上的位置
507 buttoninsert_stu.place(x=50,y=50,height=40,width=200)
508 
509 buttondelete_stu=Button(root,text="刪除學生信息",font=("微軟雅黑 -20"),command=delete_stu)
510 #buttondelete_stu.grid(row=2,column=1)
511 buttondelete_stu.place(x=50,y=150,height=40,width=200)
512 
513 buttonchange_stu=Button(root,text="修改學生信息",font=("微軟雅黑 -20"),command=change_stu)
514 #buttonchange_stu.grid(row=4,column=0)
515 buttonchange_stu.place(x=50,y=250,height=40,width=200)
516 
517 buttonsel_stu=Button(root,text="查詢學生信息",font=("微軟雅黑 -20"),command=sel_stu)
518 #buttonsel_stu.grid(row=4,column=1)
519 buttonsel_stu.place(x=50,y=350,height=40,width=200)
520 root.mainloop()

適用於初學python的學者。能夠了解python的界面設計和對數據庫的操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM