当前位置:   article > 正文

Python+Tkinter+MySQL带你搭建学生信息管理系统界面_python+tkinter+mysqltu

python+tkinter+mysqltu

一、Tkinter

Tkinter是Python中的一个标准GUI库,用于创建图形用户界面(GUI)。它是基于Tcl/Tk工具包构建的,提供了一种简单而有效的方式来创建应用程序的用户界面。

以下是Tkinter的一些重要特性和组件:

  1. 简单易用:Tkinter提供了简单的API,使得创建GUI应用程序变得容易。它提供了许多用于构建各种GUI元素的类和方法。

  2. 跨平台性:Tkinter是Python标准库的一部分,因此它可以在几乎所有的主流操作系统上运行,包括Windows、macOS和Linux。

  3. 组件丰富:Tkinter提供了许多常用的GUI组件,如按钮、标签、文本框、复选框、单选按钮、列表框、滚动条等,以及各种布局管理器,如pack、grid和place。

  4. 事件驱动:Tkinter是事件驱动的,可以通过绑定事件处理函数来响应用户的操作,例如单击按钮、按下键盘等。

  5. 可扩展性:虽然Tkinter提供了大量的标准组件,但也可以通过自定义组件或使用第三方库来扩展其功能。

Tkinter的工作流程通常包括以下步骤:

  • 创建主窗口(root window):使用Tkinter创建一个主窗口作为整个应用程序的主界面。
  • 添加组件:在主窗口中添加所需的GUI组件,例如按钮、标签、文本框等。
  • 配置组件属性:设置每个组件的属性,如文本内容、大小、位置等。
  • 布局管理:使用布局管理器来安排组件的位置和大小,以确保它们在窗口中正确显示。
  • 事件处理:为需要交互的组件绑定事件处理函数,以响应用户的操作。
  • 运行应用程序:启动事件循环,等待用户与应用程序交互。

总的来说,Tkinter是Python中创建GUI应用程序的一种方便而强大的方式,特别适用于小型项目或需要快速原型设计的应用程序。

二、代码实现

1、登录窗口

  1. from tkinter import *
  2. import tkinter.messagebox
  3. # 导入数据库链接模块
  4. from connect_mysql import conn
  5. # 导入学生管理功能模块
  6. from MainPage import Function
  7. # 创建数据库操作对象并链接数据库
  8. cursor = conn.cursor()
  9. # 创建窗口和函数
  10. class Main:
  11. # 窗口初始化
  12. def __init__(self):
  13. self.window = Tk()
  14. self.window.title('学生信息管理系统')
  15. self.window.geometry('600x400')
  16. self.window.resizable(False, False)
  17. self.canvas = Canvas(self.window, height=400, width=600)
  18. self.bg_image = PhotoImage(file="image/bg1.png")
  19. self.canvas.pack(side='top')
  20. self.image = self.canvas.create_image(0, 0, anchor='nw', image=self.bg_image)
  21. self.lb1 = Label(self.window, text='用户名:', width=10, height=2).place(x=200, y=150)
  22. self.lb2 = Label(self.window, text='密 码:', width=10, height=2).place(x=200, y=200)
  23. self.get_user_name = StringVar()
  24. self.en_user_name = Entry(self.window, textvariable=self.get_user_name)
  25. self.en_user_name.place(x=260, y=150, height=40)
  26. self.get_user_pwd = StringVar()
  27. self.en_user_pwd = Entry(self.window, textvariable=self.get_user_pwd, show='*')
  28. self.en_user_pwd.place(x=260, y=200, height=40)
  29. self.bt_login = Button(self.window, text='登录', command=self.user_login, width=10, height=2)
  30. self.bt_login.place(x=150, y=300)
  31. self.bt_logup = Button(self.window, text='注册', command=self.user_register, width=10, height=2)
  32. self.bt_logup.place(x=250, y=300)
  33. self.bt_quit = Button(self.window, text='退出', command=self.window.quit, width=10, height=2)
  34. self.bt_quit.place(x=350, y=300)
  35. self.window.mainloop()

2、准备数据库

新建一个数据库,再建一个user表,分别添加username与password字段。

3、编写数据库连接代码

  1. from pymysql import Connection
  2. # 自己的主机名、用户名、密码
  3. conn = Connection(
  4. host="localhost",
  5. port=3306,
  6. user="root",
  7. password="123456",
  8. autocommit=True
  9. )
  10. # 获取游标对象
  11. cursor = conn.cursor()
  12. # 选择数据库
  13. conn.select_db("db_student")
  14. # 执行sql语句
  15. cursor.execute("select * from user ")
  16. # 输出结果
  17. result = cursor.fetchall()
  18. # 控制台打印结果
  19. print(result)

4、主页面编写

  1. # Import required modules
  2. import tkinter
  3. from tkinter import *
  4. from tkinter import ttk, messagebox
  5. from connect_mysql import conn
  6. # connect database
  7. cursor = conn.cursor()
  8. # Initialization of the main interface
  9. class Function:
  10. # Interface design
  11. def __init__(self):
  12. self.window = Tk()
  13. self.window.title('学生信息管理系统')
  14. self.window.geometry('750x450')
  15. self.canvas = Canvas(self.window, height=450, width=750)
  16. self.bg_image = PhotoImage(file="image/bg2.png")
  17. self.window.resizable(False, False)
  18. self.canvas.pack(side='top')
  19. self.image = self.canvas.create_image(0, 0, anchor='nw', image=self.bg_image)
  20. bt1 = Button(self.window, text='添加信息', command=self.insertItem, width=20, height=2)
  21. bt1.place(x=30, y=190)
  22. bt2 = Button(self.window, text='删除信息', command=self.deleteItem, width=20, height=2)
  23. bt2.place(x=30, y=260)
  24. bt3 = Button(self.window, text='修改信息', command=self.updateItem, width=20, height=2)
  25. bt3.place(x=30, y=330)
  26. lb4 = Label(self.window, text='学号:', width=7, height=2).place(x=200, y=20)
  27. lb5 = Label(self.window, text='姓名:', width=7, height=2).place(x=200, y=70)
  28. lb6 = Label(self.window, text='班级:', width=7, height=2).place(x=200, y=120)
  29. bt4 = Button(self.window, text=' 查找 ', command=self.search, width=10, height=2).place(
  30. x=450, y=120)
  31. bt5 = Button(self.window, text="退出", command=self.exit, width=10, height=2)
  32. bt5.place(x=550, y=120)
  33. bt6 = Button(self.window, text="刷新", command=self.refresh, width=10, height=2)
  34. bt6.place(x=650, y=120)
  35. self.sdut_id = StringVar()
  36. self.en_sdut_id = Entry(self.window, textvariable=self.sdut_id)
  37. self.en_sdut_id.place(x=270, y=20, height=40)
  38. self.sdut_name = StringVar()
  39. self.en_sdut_name = Entry(self.window, textvariable=self.sdut_name)
  40. self.en_sdut_name.place(x=270, y=70, height=40)
  41. self.sdut_class = StringVar()
  42. self.en_sdut_class = Entry(self.window, textvariable=self.sdut_class)
  43. self.en_sdut_class.place(x=270, y=120, height=40)
  44. self.tree = ttk.Treeview(self.window, show="headings")
  45. self.tree["columns"] = ("0", "1", "2", "3", "4", "5")
  46. self.tree.place(x=200, y=180)
  47. self.tree.column('0', width=90, anchor='center')
  48. self.tree.column('1', width=90, anchor='center')
  49. self.tree.column('2', width=90, anchor='center')
  50. self.tree.column('3', width=90, anchor='center')
  51. self.tree.column('4', width=90, anchor='center')
  52. self.tree.column('5', width=90, anchor='center')
  53. self.tree.heading('0', text='学号')
  54. self.tree.heading('1', text='姓名')
  55. self.tree.heading('2', text='性别')
  56. self.tree.heading('3', text='年龄')
  57. self.tree.heading('4', text='班级')
  58. self.tree.heading('5', text='成绩')
  59. result = self.show()
  60. for res in result:
  61. li = [res[0], res[1], res[2], res[3], res[4], res[5]]
  62. self.tree.insert('', 'end', values=li)
  63. self.window.mainloop()

三、系统展示

1、登录界面

2、注册页面

 3、主页面

 4、添加信息页面

 5、修改信息页面

四、源码获取

需要源码的联系方式如下:

  1. QQ:1633182048
  2. Wechat:ljx56260937

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/716337
推荐阅读
相关标签
  

闽ICP备14008679号