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 相关文章推荐
Python中__init__和__new__的区别详解
Jul 09 Python
整理Python最基本的操作字典的方法
Apr 24 Python
星球大战与Python之间的那些事
Jan 07 Python
Python中线程的MQ消息队列实现以及消息队列的优点解析
Jun 29 Python
python虚拟环境virualenv的安装与使用
Dec 18 Python
Python入门之三角函数全解【收藏】
Nov 08 Python
django的登录注册系统的示例代码
May 14 Python
django数据库自动重连的方法实例
Jul 21 Python
Python在终端通过pip安装好包以后在Pycharm中依然无法使用的问题(三种解决方案)
Mar 10 Python
基于Python实现2种反转链表方法代码实例
Jul 06 Python
Python OpenCV 图像平移的实现示例
Jun 04 Python
python基础之类方法和静态方法
Oct 24 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
也谈截取首页新闻 - 范例
2006/10/09 PHP
简单实现PHP留言板功能
2016/12/21 PHP
thinkphp修改配置进入默认首页的方法
2017/02/07 PHP
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解
2017/11/14 PHP
PHP 实现文件压缩解压操作的方法
2019/06/14 PHP
你所要知道JS(DHTML)中的一些技巧
2007/01/09 Javascript
Javascript条件判断使用小技巧总结
2008/09/08 Javascript
javascript 操作select下拉列表框的一点小经验
2010/03/20 Javascript
javascript下数值型比较难点说明
2010/06/07 Javascript
jquery animate实现鼠标放上去显示离开隐藏效果
2013/07/21 Javascript
解析offsetHeight,clientHeight,scrollHeight之间的区别
2013/11/20 Javascript
jquery如何把数组变为字符串传到服务端并处理
2014/04/30 Javascript
jQuery实现ctrl+enter(回车)提交表单
2015/10/19 Javascript
jquery实现无刷新验证码的简单实例
2016/05/19 Javascript
详解Vue 开发模式下跨域问题
2017/06/06 Javascript
vue使用vue-cli快速创建工程
2017/07/28 Javascript
react router 4.0以上的路由应用详解
2017/09/21 Javascript
把vue-router和express项目部署到服务器的方法
2018/02/21 Javascript
vue使用v-if v-show页面闪烁,div闪现的解决方法
2018/10/12 Javascript
Vue的v-model的几种修饰符.lazy,.number和.trim的用法说明
2020/08/05 Javascript
Ant Design moment对象和字符串之间的相互转化教程
2020/10/27 Javascript
自己使用总结Python程序代码片段
2015/06/02 Python
Python构造自定义方法来美化字典结构输出的示例
2016/06/16 Python
修复 Django migration 时遇到的问题解决
2018/06/14 Python
Python time库基本使用方法分析
2019/12/13 Python
Python 实现递归法解决迷宫问题的示例代码
2020/01/12 Python
python如何进入交互模式
2020/07/06 Python
Python使用itcaht库实现微信自动收发消息功能
2020/07/13 Python
pytest fixtures装饰器的使用和如何控制用例的执行顺序
2021/01/28 Python
html5+css3之CSS中的布局与Header的实现
2014/11/21 HTML / CSS
英国Amara家居法国网站:家居装饰,现代装饰和豪华礼品
2016/12/15 全球购物
任命书范本大全
2014/06/06 职场文书
销售内勤岗位职责
2015/02/10 职场文书
Python代码风格与编程习惯重要吗?
2021/06/03 Python
使用Mysql计算地址的经纬度距离和实时位置信息
2022/04/29 MySQL
win10蓝屏0xc0000001安全模式进不了怎么办?win10出现0xc0000001的解决方法
2022/08/05 数码科技