python+tkinter实现学生管理系统


Posted in Python onAugust 20, 2019

本文实例为大家分享了python+tkinter实现学生管理系统的具体代码,供大家参考,具体内容如下 

python+tkinter实现学生管理系统

from tkinter import *
from tkinter.messagebox import *
import sqlite3
from tkinter import ttk
 
dbstr = "H:\mydb.db"
 
root = Tk()
root.geometry('700x1000')
root.title('学生管理系统')
 
Label(root, text="学号:").place(relx=0, rely=0.05, relwidth=0.1)
Label(root, text="姓名:").place(relx=0.5, rely=0.05, relwidth=0.1)
Label(root, text="电话:").place(relx=0, rely=0.1, relwidth=0.1)
Label(root, text="地址:").place(relx=0.5, rely=0.1, relwidth=0.1)
 
sid = StringVar()
name = StringVar()
phone = StringVar()
address = StringVar()
Entry(root, textvariable=sid).place(relx=0.1, rely=0.05, relwidth=0.37, height=25)
Entry(root, textvariable=name).place(relx=0.6, rely=0.05, relwidth=0.37, height=25)
 
Entry(root, textvariable=phone).place(relx=0.1, rely=0.1, relwidth=0.37, height=25)
Entry(root, textvariable=address).place(relx=0.6, rely=0.1, relwidth=0.37, height=25)
 
Label(root, text='学生信息管理', bg='white', fg='red', font=('宋体', 15)).pack(side=TOP, fill='x')
 
 
def showAllInfo():
 x = dataTreeview.get_children()
 for item in x:
  dataTreeview.delete(item)
 con = sqlite3.connect(dbstr)
 cur = con.cursor()
 cur.execute("select * from student")
 lst = cur.fetchall()
 for item in lst:
  dataTreeview.insert("", 1, text="line1", values=item)
 cur.close()
 con.close()
 
 
def appendInfo():
 if sid.get() == "":
  showerror(title='提示', message='输入不能为空')
 elif name.get() == "":
  showerror(title='提示', message='输入不能为空')
 elif phone.get() == "":
  showerror(title='提示', message='输入不能为空')
 elif address.get() == "":
  showerror(title='提示', message='输入不能为空')
 else:
  x = dataTreeview.get_children()
  for item in x:
   dataTreeview.delete(item)
  list1 = []
  list1.append(sid.get())
  list1.append(name.get())
  list1.append(phone.get())
  list1.append(address.get())
  con = sqlite3.connect(dbstr)
  cur = con.cursor()
  cur.execute("insert into student values(?,?,?,?)", tuple(list1))
  con.commit()
  cur.execute("select * from student")
  lst = cur.fetchall()
  for item in lst:
   dataTreeview.insert("", 1, text="line1", values=item)
  cur.close()
  con.close()
 
 
def deleteInfo():
 con = sqlite3.connect(dbstr)
 cur = con.cursor()
 cur.execute("select * from student")
 studentList = cur.fetchall()
 cur.close()
 con.close()
 print(studentList)
 
 num = sid.get()
 flag = 0
 if num.isnumeric() == False:
  showerror(title='提示', message='删除失败')
 for i in range(len(studentList)):
  for item in studentList[i]:
   if int(num) == item:
    flag = 1
    con = sqlite3.connect(dbstr)
    cur = con.cursor()
    cur.execute("delete from student where id = ?", (int(num),))
    con.commit()
    cur.close()
    con.close()
    break
 if flag == 1:
  showinfo(title='提示', message='删除成功!')
 else:
  showerror(title='提示', message='删除失败')
 
 x = dataTreeview.get_children()
 for item in x:
  dataTreeview.delete(item)
 
 con = sqlite3.connect(dbstr)
 cur = con.cursor()
 cur.execute("select * from student")
 lst = cur.fetchall()
 for item in lst:
  dataTreeview.insert("", 1, text="line1", values=item)
 cur.close()
 con.close()
 
 
Button(root, text="显示所有信息", command=showAllInfo).place(relx=0.2, rely=0.2, width=100)
Button(root, text="追加信息", command=appendInfo).place(relx=0.4, rely=0.2, width=100)
Button(root, text="删除信息", command=deleteInfo).place(relx=0.6, rely=0.2, width=100)
 
 
dataTreeview = ttk.Treeview(root, show='headings', column=('sid', 'name', 'phone', 'address'))
dataTreeview.column('sid', width=150, anchor="center")
dataTreeview.column('name', width=150, anchor="center")
dataTreeview.column('phone', width=150, anchor="center")
dataTreeview.column('address', width=150, anchor="center")
 
dataTreeview.heading('sid', text='学号')
dataTreeview.heading('name', text='名字')
dataTreeview.heading('phone', text='电话')
dataTreeview.heading('address', text='地址')
 
dataTreeview.place(rely=0.3, relwidth=0.97)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python3+PyQt5使用数据库表视图
Apr 24 Python
Python装饰器知识点补充
May 28 Python
python3 爬取图片的实例代码
Nov 06 Python
Python爬虫实现获取动态gif格式搞笑图片的方法示例
Dec 24 Python
Python简直是万能的,这5大主要用途你一定要知道!(推荐)
Apr 03 Python
python实现切割url得到域名、协议、主机名等各个字段的例子
Jul 25 Python
scrapy爬虫:scrapy.FormRequest中formdata参数详解
Apr 30 Python
python学习将数据写入文件并保存方法
Jun 07 Python
Python生成随机验证码代码实例解析
Jun 09 Python
详解Python 函数参数的拆解
Sep 02 Python
python删除csv文件的行列
Apr 06 Python
python中requests库+xpath+lxml简单使用
Apr 29 Python
Python对列表的操作知识点详解
Aug 20 #Python
python中的global关键字的使用方法
Aug 20 #Python
python并发编程 Process对象的其他属性方法join方法详解
Aug 20 #Python
浅谈pytorch grad_fn以及权重梯度不更新的问题
Aug 20 #Python
解决Pytorch 训练与测试时爆显存(out of memory)的问题
Aug 20 #Python
python中用logging实现日志滚动和过期日志删除功能
Aug 20 #Python
python3中替换python2中cmp函数的实现
Aug 20 #Python
You might like
浅析PHP中Collection 类的设计
2013/06/21 PHP
安装apache2.2.22配置php5.4(具体操作步骤)
2013/06/26 PHP
php绘制一个矩形的方法
2015/01/24 PHP
基于PHP实现短信验证码接口(容联运通讯)
2016/09/06 PHP
基于jquery实现的省市区级联无ajax
2013/09/24 Javascript
js判断当前浏览器类型,判断IE浏览器方法
2014/06/02 Javascript
jQuery插件Slider Revolution实现响应动画滑动图片切换效果
2015/06/05 Javascript
js实现的后台左侧管理菜单代码
2015/09/11 Javascript
基于jquery实现智能表单验证操作
2016/05/09 Javascript
js 实现数值的千分位及保存小数方法(推荐)
2016/08/01 Javascript
纯js和css完成贪吃蛇小游戏demo
2016/09/01 Javascript
jQuery插件HighCharts绘制2D柱状图、折线图和饼图的组合图效果示例【附demo源码下载】
2017/03/09 Javascript
jquery 手势密码插件
2017/03/17 Javascript
layui实现点击按钮给table添加一行
2018/08/10 Javascript
微信小程序全局变量功能与用法详解
2019/01/22 Javascript
JS利用prototype给类添加方法操作详解
2019/06/21 Javascript
vue+elementUi 实现密码显示/隐藏+小图标变化功能
2020/01/18 Javascript
使用vue打包进行云服务器上传的问题
2020/03/02 Javascript
小程序中的箭头函数的具体使用
2020/06/19 Javascript
python自动zip压缩目录的方法
2015/06/28 Python
Python装饰器实现几类验证功能做法实例
2017/05/18 Python
用python 实现在不确定行数情况下多行输入方法
2019/01/28 Python
教你如何编写、保存与运行Python程序的方法
2019/07/12 Python
django rest framework 实现用户登录认证详解
2019/07/29 Python
对Django的restful用法详解(自带的增删改查)
2019/08/28 Python
Tensorflow实现将标签变为one-hot形式
2020/05/22 Python
Python数据库封装实现代码示例解析
2020/09/05 Python
最新Python idle下载、安装与使用教程图文详解
2020/11/28 Python
CSS3 三维变形实现立体方块特效源码
2016/12/15 HTML / CSS
HTML5 Web Workers之网站也能多线程的实现
2013/04/24 HTML / CSS
基于Html5 canvas实现裁剪图片和马赛克功能及又拍云上传图片 功能
2019/07/09 HTML / CSS
canvas绘制树形结构可视图形的实现
2020/04/03 HTML / CSS
房地产还款计划书
2014/01/10 职场文书
《一本男孩子必读的书》教学反思
2014/02/19 职场文书
北京奥运会主题口号
2014/06/13 职场文书
MySQL Router实现MySQL的读写分离的方法
2021/05/27 MySQL