赞
踩
Tkinter是Python中的一个标准GUI库,用于创建图形用户界面(GUI)。它是基于Tcl/Tk工具包构建的,提供了一种简单而有效的方式来创建应用程序的用户界面。
以下是Tkinter的一些重要特性和组件:
简单易用:Tkinter提供了简单的API,使得创建GUI应用程序变得容易。它提供了许多用于构建各种GUI元素的类和方法。
跨平台性:Tkinter是Python标准库的一部分,因此它可以在几乎所有的主流操作系统上运行,包括Windows、macOS和Linux。
组件丰富:Tkinter提供了许多常用的GUI组件,如按钮、标签、文本框、复选框、单选按钮、列表框、滚动条等,以及各种布局管理器,如pack、grid和place。
事件驱动:Tkinter是事件驱动的,可以通过绑定事件处理函数来响应用户的操作,例如单击按钮、按下键盘等。
可扩展性:虽然Tkinter提供了大量的标准组件,但也可以通过自定义组件或使用第三方库来扩展其功能。
Tkinter的工作流程通常包括以下步骤:
总的来说,Tkinter是Python中创建GUI应用程序的一种方便而强大的方式,特别适用于小型项目或需要快速原型设计的应用程序。
- from tkinter import *
- import tkinter.messagebox
-
- # 导入数据库链接模块
- from connect_mysql import conn
- # 导入学生管理功能模块
- from MainPage import Function
- # 创建数据库操作对象并链接数据库
- cursor = conn.cursor()
- # 创建窗口和函数
- class Main:
- # 窗口初始化
- def __init__(self):
- self.window = Tk()
- self.window.title('学生信息管理系统')
- self.window.geometry('600x400')
- self.window.resizable(False, False)
- self.canvas = Canvas(self.window, height=400, width=600)
- self.bg_image = PhotoImage(file="image/bg1.png")
- self.canvas.pack(side='top')
- self.image = self.canvas.create_image(0, 0, anchor='nw', image=self.bg_image)
- self.lb1 = Label(self.window, text='用户名:', width=10, height=2).place(x=200, y=150)
- self.lb2 = Label(self.window, text='密 码:', width=10, height=2).place(x=200, y=200)
- self.get_user_name = StringVar()
- self.en_user_name = Entry(self.window, textvariable=self.get_user_name)
- self.en_user_name.place(x=260, y=150, height=40)
- self.get_user_pwd = StringVar()
- self.en_user_pwd = Entry(self.window, textvariable=self.get_user_pwd, show='*')
- self.en_user_pwd.place(x=260, y=200, height=40)
- self.bt_login = Button(self.window, text='登录', command=self.user_login, width=10, height=2)
- self.bt_login.place(x=150, y=300)
- self.bt_logup = Button(self.window, text='注册', command=self.user_register, width=10, height=2)
- self.bt_logup.place(x=250, y=300)
- self.bt_quit = Button(self.window, text='退出', command=self.window.quit, width=10, height=2)
- self.bt_quit.place(x=350, y=300)
- self.window.mainloop()

新建一个数据库,再建一个user表,分别添加username与password字段。
- from pymysql import Connection
- # 自己的主机名、用户名、密码
- conn = Connection(
- host="localhost",
- port=3306,
- user="root",
- password="123456",
- autocommit=True
- )
- # 获取游标对象
- cursor = conn.cursor()
- # 选择数据库
- conn.select_db("db_student")
- # 执行sql语句
- cursor.execute("select * from user ")
- # 输出结果
- result = cursor.fetchall()
- # 控制台打印结果
- print(result)
-

- # Import required modules
- import tkinter
- from tkinter import *
- from tkinter import ttk, messagebox
- from connect_mysql import conn
-
- # connect database
- cursor = conn.cursor()
-
- # Initialization of the main interface
- class Function:
- # Interface design
- def __init__(self):
- self.window = Tk()
- self.window.title('学生信息管理系统')
- self.window.geometry('750x450')
- self.canvas = Canvas(self.window, height=450, width=750)
- self.bg_image = PhotoImage(file="image/bg2.png")
- self.window.resizable(False, False)
- self.canvas.pack(side='top')
- self.image = self.canvas.create_image(0, 0, anchor='nw', image=self.bg_image)
- bt1 = Button(self.window, text='添加信息', command=self.insertItem, width=20, height=2)
- bt1.place(x=30, y=190)
- bt2 = Button(self.window, text='删除信息', command=self.deleteItem, width=20, height=2)
- bt2.place(x=30, y=260)
- bt3 = Button(self.window, text='修改信息', command=self.updateItem, width=20, height=2)
- bt3.place(x=30, y=330)
- lb4 = Label(self.window, text='学号:', width=7, height=2).place(x=200, y=20)
- lb5 = Label(self.window, text='姓名:', width=7, height=2).place(x=200, y=70)
- lb6 = Label(self.window, text='班级:', width=7, height=2).place(x=200, y=120)
- bt4 = Button(self.window, text=' 查找 ', command=self.search, width=10, height=2).place(
- x=450, y=120)
- bt5 = Button(self.window, text="退出", command=self.exit, width=10, height=2)
- bt5.place(x=550, y=120)
-
- bt6 = Button(self.window, text="刷新", command=self.refresh, width=10, height=2)
- bt6.place(x=650, y=120)
-
- self.sdut_id = StringVar()
- self.en_sdut_id = Entry(self.window, textvariable=self.sdut_id)
- self.en_sdut_id.place(x=270, y=20, height=40)
-
- self.sdut_name = StringVar()
- self.en_sdut_name = Entry(self.window, textvariable=self.sdut_name)
- self.en_sdut_name.place(x=270, y=70, height=40)
-
- self.sdut_class = StringVar()
- self.en_sdut_class = Entry(self.window, textvariable=self.sdut_class)
- self.en_sdut_class.place(x=270, y=120, height=40)
-
- self.tree = ttk.Treeview(self.window, show="headings")
- self.tree["columns"] = ("0", "1", "2", "3", "4", "5")
- self.tree.place(x=200, y=180)
- self.tree.column('0', width=90, anchor='center')
- self.tree.column('1', width=90, anchor='center')
- self.tree.column('2', width=90, anchor='center')
- self.tree.column('3', width=90, anchor='center')
- self.tree.column('4', width=90, anchor='center')
- self.tree.column('5', width=90, anchor='center')
- self.tree.heading('0', text='学号')
- self.tree.heading('1', text='姓名')
- self.tree.heading('2', text='性别')
- self.tree.heading('3', text='年龄')
- self.tree.heading('4', text='班级')
- self.tree.heading('5', text='成绩')
- result = self.show()
- for res in result:
- li = [res[0], res[1], res[2], res[3], res[4], res[5]]
- self.tree.insert('', 'end', values=li)
- self.window.mainloop()

需要源码的联系方式如下:
- QQ:1633182048
-
- Wechat:ljx56260937
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。