传奇开心果微博系列
- 系列微博目录
- Python微项目技术点案例示例系列
- 微博目录
- 前言
- 一、主窗口和子窗口创建和切换,以员工信息管理系统示例代码
- 二、主窗口添加有菜单项图标的菜单栏、工具栏和右键菜单示例代码
- 三、使用sqlite3数据库增删改查管理员工信息示例代码
- 四、在主界面增加增删改查实现相关功能,创建增删改查显示子窗口示例代码
- 五、增加模糊查询功能示例代码
- 六、增加登录验证功能示例代码
- 七、增加权限控制示例代码
- 八、实现比较完整漂亮美观的员工管理信息系统示例代码
- 九、归纳总结
系列微博目录
Python微项目技术点案例示例系列
微博目录
前言
Tkinter.ttk是一个用于创建现代化用户界面的Tkinter的扩展模块。它提供了一组新的小部件,这些小部件具有更好的外观和功能,可以让您创建更具吸引力和专业的界面。
Tkinter.ttk提供的小部件包括按钮、标签、进度条、滚动条、树状视图等,这些小部件与标准的Tkinter小部件相比具有更丰富的样式和主题选项。此外,Tkinter.ttk还提供了一些额外的功能,如内置的主题支持、状态指示器、键盘导航等,使得界面设计更加灵活和易用。
总的来说,Tkinter.ttk是一个强大的工具,可以帮助您创建更加现代化和专业的用户界面,提升用户体验和界面设计的质量。
一、主窗口和子窗口创建和切换,以员工信息管理系统示例代码
在Tkinter中,可以使用ttk模块创建主窗口和子窗口,并实现它们之间的切换。下面是一个示例代码,演示了如何创建一个简单的员工信息管理系统,包括主窗口和子窗口的创建和切换:
import tkinter as tk
from tkinter import ttk
def show_add_employee_window():
add_employee_window = tk.Toplevel(root)
add_employee_window.title("Add Employee")
add_employee_window.geometry("200x100")
label = ttk.Label(add_employee_window, text="Enter employee name:")
label.pack()
entry = ttk.Entry(add_employee_window)
entry.pack()
button = ttk.Button(add_employee_window, text="Add Employee")
button.pack()
def show_main_window():
main_window = tk.Toplevel(root)
main_window.title("Employee Management System")
main_window.geometry("300x200")
label = ttk.Label(main_window, text="Welcome to the Employee Management System")
label.pack()
button = ttk.Button(main_window, text="Add Employee", command=show_add_employee_window)
button.pack()
root = tk.Tk()
root.title("Employee Management System")
root.geometry("400x300")
show_main_window()
root.mainloop()
在这个示例代码中,首先创建了一个主窗口root,并调用show_main_window()函数显示主窗口内容。在主窗口中,有一个欢迎信息和一个“Add Employee”按钮,点击按钮会调用show_add_employee_window()函数显示一个子窗口用于添加员工信息。
show_add_employee_window()函数创建一个子窗口add_employee_window,包含一个标签、一个输入框和一个按钮,用于输入员工姓名并添加到系统中。
通过这种方式,可以实现主窗口和子窗口之间的切换,从而实现员工信息管理系统的功能。您可以根据需要进一步扩展和优化这个示例代码。
二、主窗口添加有菜单项图标的菜单栏、工具栏和右键菜单示例代码
以下是一个示例代码,演示了如何在Tkinter的主窗口中添加带有菜单项图标的菜单栏、工具栏和右键菜单:
import tkinter as tk
from tkinter import ttk
def on_right_click(event):
right_click_menu.post(event.x_root, event.y_root)
root = tk.Tk()
root.title("Menu Example")
root.geometry("400x300")
# 创建菜单栏
menu_bar = tk.Menu(root)
# 创建文件菜单
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New", compound=tk.LEFT, image=None)
file_menu.add_command(label="Open", compound=tk.LEFT, image=None)
file_menu.add_separator()
file_menu.add_command(label="Exit", compound=tk.LEFT, image=None, command=root.quit)
menu_bar.add_cascade(label="File", menu=file_menu)
root.config(menu=menu_bar)
# 创建工具栏
tool_bar = ttk.Frame(root)
tool_bar.pack(side=tk.TOP, fill=tk.X)
new_button = ttk.Button(tool_bar, text="New", compound=tk.LEFT, image=None)
new_button.pack(side=tk.LEFT)
open_button = ttk.Button(tool_bar, text="Open", compound=tk.LEFT, image=None)
open_button.pack(side=tk.LEFT)
exit_button = ttk.Button(tool_bar, text="Exit", compound=tk.LEFT, image=None, command=root.quit)
exit_button.pack(side=tk.LEFT)
# 创建右键菜单
right_click_menu = tk.Menu(root, tearoff=0)
right_click_menu.add_command(label="Cut")
right_click_menu.add_command(label="Copy")
right_click_menu.add_command(label="Paste")
root.bind("<Button-3>", on_right_click)
root.mainloop()
在这个示例代码中,首先创建了一个主窗口root,并添加了一个带有菜单项图标的菜单栏、工具栏和右键菜单。菜单栏包括一个文件菜单,工具栏包括三个按钮(New、Open、Exit),右键菜单包括三个选项(Cut、Copy、Paste)。
在右键菜单部分,通过绑定鼠标右键事件""来触发右键菜单的显示。当用户在主窗口上右键单击时,右键菜单会在鼠标位置显示出来。
您可以根据需要进一步扩展和优化这个示例代码,以满足您的具体需求。
三、使用sqlite3数据库增删改查管理员工信息示例代码
以下是一个示例代码,演示了如何使用SQLite3数据库来增加、删除、修改和查询员工信息的功能:
import sqlite3
# 连接到SQLite数据库
conn = sqlite3.connect('employee.db')
c = conn.cursor()
# 创建员工表
c.execute('''CREATE TABLE IF NOT EXISTS employees
(id INTEGER PRIMARY KEY, name TEXT, position TEXT, department TEXT)''')
# 添加员工信息
def add_employee(name, position, department):
c.execute("INSERT INTO employees (name, position, department) VALUES (?, ?, ?)", (name, position, department))
conn.commit()
print("Employee added successfully")
# 删除员工信息
def delete_employee(employee_id):
c.execute("DELETE FROM employees WHERE id=?", (employee_id,))
conn.commit()
print("Employee deleted successfully")
# 更新员工信息
def update_employee(employee_id, name, position, department):
c.execute("UPDATE employees SET name=?, position=?, department=? WHERE id=?", (name, position, department, employee_id))
conn.commit()
print("Employee updated successfully")
# 查询所有员工信息
def get_all_employees():
c.execute("SELECT * FROM employees")
employees = c.fetchall()
for employee in employees:
print(employee)
# 添加员工信息
add_employee("John Doe", "Manager", "Sales")
# 查询所有员工信息
get_all_employees()
# 更新员工信息
update_employee(1, "John Smith", "Senior Manager", "Sales")
# 查询所有员工信息
get_all_employees()
# 删除员工信息
delete_employee(1)
# 查询所有员工信息
get_all_employees()
# 关闭数据库连接
conn.close()
在这个示例代码中,首先连接到名为employee.db
的SQLite数据库,并创建了一个名为employees
的员工表。然后定义了添加员工信息、删除员工信息、更新员工信息和查询所有员工信息的函数。
接着依次演示了添加员工信息、查询所有员工信息、更新员工信息、查询所有员工信息、删除员工信息和查询所有员工信息的过程。
您可以根据需要进一步扩展和优化这个示例代码,以满足您的具体需求。
四、在主界面增加增删改查实现相关功能,创建增删改查显示子窗口示例代码
以下是一个示例代码,演示了如何在主界面中增加按钮来打开子窗口,实现员工信息的增删改查功能:
import tkinter as tk
import sqlite3
from tkinter import messagebox
def add_employee_window():
add_window = tk.Toplevel(root)
add_window.title("Add Employee")
name_label = tk.Label(add_window, text="Name:")
name_label.pack()
name_entry = tk.Entry(add_window)
name_entry.pack()
position_label = tk.Label(add_window, text="Position:")
position_label.pack()
position_entry = tk.Entry(add_window)
position_entry.pack()
department_label = tk.Label(add_window, text="Department:")
department_label.pack()
department_entry = tk.Entry(add_window)
department_entry.pack()
def add_employee_to_db():
name = name_entry.get()
position = position_entry.get()
department = department_entry.get()
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("INSERT INTO employees (name, position, department) VALUES (?, ?, ?)", (name, position, department))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Employee added successfully")
add_window.destroy()
add_button = tk.Button(add_window, text="Add Employee", command=add_employee_to_db)
add_button.pack()
def delete_employee_window():
delete_window = tk.Toplevel(root)
delete_window.title("Delete Employee")
id_label = tk.Label(delete_window, text="Employee ID:")
id_label.pack()
id_entry = tk.Entry(delete_window)
id_entry.pack()
def delete_employee_from_db():
employee_id = id_entry.get()
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("DELETE FROM employees WHERE id=?", (employee_id,))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Employee deleted successfully")
delete_window.destroy()
delete_button = tk.Button(delete_window, text="Delete Employee", command=delete_employee_from_db)
delete_button.pack()
def update_employee_window():
update_window = tk.Toplevel(root)
update_window.title("Update Employee")
id_label = tk.Label(update_window, text="Employee ID:")
id_label.pack()
id_entry = tk.Entry(update_window)
id_entry.pack()
name_label = tk.Label(update_window, text="Name:")
name_label.pack()
name_entry = tk.Entry(update_window)
name_entry.pack()
position_label = tk.Label(update_window, text="Position:")
position_label.pack()
position_entry = tk.Entry(update_window)
position_entry.pack()
department_label = tk.Label(update_window, text="Department:")
department_label.pack()
department_entry = tk.Entry(update_window)
department_entry.pack()
def update_employee_in_db():
employee_id = id_entry.get()
name = name_entry.get()
position = position_entry.get()
department = department_entry.get()
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("UPDATE employees SET name=?, position=?, department=? WHERE id=?", (name, position, department, employee_id))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Employee updated successfully")
update_window.destroy()
update_button = tk.Button(update_window, text="Update Employee", command=update_employee_in_db)
update_button.pack()
def display_employees():
display_window = tk.Toplevel(root)
display_window.title("Employees")
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("SELECT * FROM employees")
employees = c.fetchall()
conn.close()
for employee in employees:
tk.Label(display_window, text=employee).pack()
root = tk.Tk()
root.title("Employee Management System")
add_button = tk.Button(root, text="Add Employee", command=add_employee_window)
add_button.pack()
delete_button = tk.Button(root, text="Delete Employee", command=delete_employee_window)
delete_button.pack()
update_button = tk.Button(root, text="Update Employee", command=update_employee_window)
update_button.pack()
display_button = tk.Button(root, text="Display Employees", command=display_employees)
display_button.pack()
root.mainloop()
在这个示例代码中,主界面包括了四个按钮:Add Employee、Delete Employee、Update Employee和Display Employees。每个按钮对应打开一个子窗口,用来实现增加、删除、修改和显示员工信息的功能。子窗口中包括相应的输入框和按钮,用户可以在子窗口中输入员工信息并进行操作。
您可以根据需要进一步扩展和优化这个示例代码,以满足您的具体需求。
五、增加模糊查询功能示例代码
为了实现具有模糊查询能力的功能,我们可以添加一个文本框供用户输入关键字,并根据输入的关键字进行模糊查询。以下是更新后的代码示例:
import tkinter as tk
import sqlite3
from tkinter import messagebox
def add_employee_window():
# 省略添加员工窗口的代码
def delete_employee_window():
# 省略删除员工窗口的代码
def update_employee_window():
# 省略更新员工窗口的代码
def display_employees():
display_window = tk.Toplevel(root)
display_window.title("Employee List")
conn = sqlite3.connect('employee.db')
c = conn.cursor()
# 获取用户输入的关键字
keyword = search_entry.get()
# 执行模糊查询
c.execute("SELECT * FROM employees WHERE name LIKE ? OR position LIKE ? OR department LIKE ?", ('%'+keyword+'%', '%'+keyword+'%', '%'+keyword+'%'))
employees = c.fetchall()
conn.close()
for employee in employees:
employee_info = f"ID: {employee[0]}, Name: {employee[1]}, Position: {employee[2]}, Department: {employee[3]}"
label = tk.Label(display_window, text=employee_info)
label.pack()
root = tk.Tk()
root.title("Employee Management System")
# 添加一个文本框用于输入关键字
search_label = tk.Label(root, text="Search:")
search_label.pack()
search_entry = tk.Entry(root)
search_entry.pack()
add_button = tk.Button(root, text="Add Employee", command=add_employee_window)
add_button.pack()
delete_button = tk.Button(root, text="Delete Employee", command=delete_employee_window)
delete_button.pack()
update_button = tk.Button(root, text="Update Employee", command=update_employee_window)
update_button.pack()
display_button = tk.Button(root, text="Display Employees", command=display_employees)
display_button.pack()
root.mainloop()
在这个更新后的代码中,我们添加了一个文本框用于输入关键字,并在display_employees
函数中使用LIKE
语句进行模糊查询。当用户点击“Display Employees”按钮时,会根据输入的关键字显示符合条件的员工信息。您可以根据需要进一步调整和完善这个功能。
六、增加登录验证功能示例代码
要添加登录验证功能,我们可以创建一个登录窗口,要求用户输入用户名和密码,并在验证成功后才允许访问员工管理系统。以下是一个示例代码,演示如何实现登录验证功能:
import tkinter as tk
import sqlite3
from tkinter import messagebox
def login_window():
login_window = tk.Toplevel(root)
login_window.title("Login")
username_label = tk.Label(login_window, text="Username:")
username_label.pack()
username_entry = tk.Entry(login_window)
username_entry.pack()
password_label = tk.Label(login_window, text="Password:")
password_label.pack()
password_entry = tk.Entry(login_window, show="*")
password_entry.pack()
def validate_login():
username = username_entry.get()
password = password_entry.get()
if username == "admin" and password == "admin123": # 假设用户名和密码为 admin/admin123
login_window.destroy()
open_employee_management()
else:
messagebox.showerror("Error", "Invalid username or password")
login_button = tk.Button(login_window, text="Login", command=validate_login)
login_button.pack()
def open_employee_management():
root.deiconify()
display_employees()
def add_employee_window():
# 省略添加员工窗口的代码
def delete_employee_window():
# 省略删除员工窗口的代码
def update_employee_window():
# 省略更新员工窗口的代码
def display_employees():
display_window = tk.Toplevel(root)
display_window.title("Employee List")
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("SELECT * FROM employees")
employees = c.fetchall()
conn.close()
for employee in employees:
employee_info = f"ID: {employee[0]}, Name: {employee[1]}, Position: {employee[2]}, Department: {employee[3]}"
label = tk.Label(display_window, text=employee_info)
label.pack()
root = tk.Tk()
root.title("Employee Management System")
root.withdraw() # 隐藏主窗口,等待用户登录
login_window()
root.mainloop()
在这个示例代码中,我们创建了一个登录窗口,要求用户输入用户名和密码。如果用户名和密码匹配成功(这里假设用户名为admin
,密码为admin123
),则关闭登录窗口并显示员工管理系统界面;否则显示错误消息框。您可以根据实际需求修改用户名和密码,并进一步完善登录验证功能。
七、增加权限控制示例代码
分为管理员权限和普通员工权限。普通员工只有查询浏览权限,管理员有增删改查和保存员工信息权限,管理员有增删改查和保存用户权限。
好的,我会为您更新示例代码,使管理员具有增删改查用户信息的权限。以下是更新后的示例代码:
import tkinter as tk
import sqlite3
from tkinter import messagebox
def login_window():
login_window = tk.Toplevel(root)
login_window.title("Login")
username_label = tk.Label(login_window, text="Username:")
username_label.pack()
username_entry = tk.Entry(login_window)
username_entry.pack()
password_label = tk.Label(login_window, text="Password:")
password_label.pack()
password_entry = tk.Entry(login_window, show="*")
password_entry.pack()
def validate_login():
username = username_entry.get()
password = password_entry.get()
conn = sqlite3.connect('user.db')
c = conn.cursor()
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
user = c.fetchone()
conn.close()
if user:
login_window.destroy()
open_employee_management(user)
else:
messagebox.showerror("Error", "Invalid username or password")
login_button = tk.Button(login_window, text="Login", command=validate_login)
login_button.pack()
def open_employee_management(user):
root.deiconify()
display_employees()
display_button = tk.Button(root, text="Display Employee Info", command=display_employees)
display_button.pack()
if user[2] == 'admin':
add_button = tk.Button(root, text="Add Employee", command=add_employee_window)
add_button.pack()
delete_button = tk.Button(root, text="Delete Employee", command=delete_employee_window)
delete_button.pack()
update_button = tk.Button(root, text="Update Employee", command=update_employee_window)
update_button.pack()
save_employee_button = tk.Button(root, text="Save Employee Info", command=save_employee_info)
save_employee_button.pack()
save_user_button = tk.Button(root, text="Save User Info", command=save_user_info)
save_user_button.pack()
display_user_button = tk.Button(root, text="Display User Info", command=display_users)
display_user_button.pack()
add_user_button = tk.Button(root, text="Add User", command=add_user_window)
add_user_button.pack()
delete_user_button = tk.Button(root, text="Delete User", command=delete_user_window)
delete_user_button.pack()
update_user_button = tk.Button(root, text="Update User", command=update_user_window)
update_user_button.pack()
def add_employee_window():
# 省略添加员工窗口的代码
def delete_employee_window():
# 省略删除员工窗口的代码
def update_employee_window():
# 省略更新员工窗口的代码
def save_employee_info():
# 省略保存员工信息的代码
def save_user_info():
# 省略保存用户信息的代码
def display_employees():
display_window = tk.Toplevel(root)
display_window.title("Employee List")
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("SELECT * FROM employees")
employees = c.fetchall()
conn.close()
for employee in employees:
employee_info = f"ID: {employee[0]}, Name: {employee[1]}, Position: {employee[2]}, Department: {employee[3]}"
label = tk.Label(display_window, text=employee_info)
label.pack()
def display_users():
display_window = tk.Toplevel(root)
display_window.title("User List")
conn = sqlite3.connect('user.db')
c = conn.cursor()
c.execute("SELECT * FROM users")
users = c.fetchall()
conn.close()
for user in users:
user_info = f"ID: {user[0]}, Username: {user[1]}, Role: {user[2], Permission: {user[3]}"
label = tk.Label(display_window, text=user_info)
label.pack()
def add_user_window():
# 省略添加用户窗口的代码
def delete_user_window():
# 省略删除用户窗口的代码
def update_user_window():
# 省略更新用户窗口的代码
root = tk.Tk()
root.title("Employee Management System")
root.withdraw() # 隐藏主窗口,等待用户登录
login_window()
root.mainloop()
八、实现比较完整漂亮美观的员工管理信息系统示例代码
下面是使用tkinter.ttk
模块写的员工管理信息系统代码示例:
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import sqlite3
def create_employee_table():
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS employees
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
position TEXT NOT NULL,
department TEXT NOT NULL)''')
conn.commit()
conn.close()
def create_user_table():
conn = sqlite3.connect('user.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL,
role TEXT NOT NULL,
permission TEXT NOT NULL)''')
conn.commit()
conn.close()
def insert_employee(name, position, department):
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("INSERT INTO employees (name, position, department) VALUES (?, ?, ?)", (name, position, department))
conn.commit()
conn.close()
def delete_employee(id):
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("DELETE FROM employees WHERE id=?", (id,))
conn.commit()
conn.close()
def update_employee(id, name, position, department):
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("UPDATE employees SET name=?, position=?, department=? WHERE id=?", (name, position, department, id))
conn.commit()
conn.close()
def display_employees():
for row in tree.get_children():
tree.delete(row)
conn = sqlite3.connect('employee.db')
c = conn.cursor()
c.execute("SELECT * FROM employees")
employees = c.fetchall()
conn.close()
for employee in employees:
tree.insert('', 'end', values=employee)
def login_window():
login_window = tk.Toplevel(root)
login_window.title("Login")
username_label = ttk.Label(login_window, text="Username:")
username_label.pack()
username_entry = ttk.Entry(login_window)
username_entry.pack()
password_label = ttk.Label(login_window, text="Password:")
password_label.pack()
password_entry = ttk.Entry(login_window, show="*")
password_entry.pack()
def validate_login():
username = username_entry.get()
password = password_entry.get()
conn = sqlite3.connect('user.db')
c = conn.cursor()
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
user = c.fetchone()
conn.close()
if user:
login_window.destroy()
open_employee_management(user)
else:
messagebox.showerror("Error", "Invalid username or password")
login_button = ttk.Button(login_window, text="Login", command=validate_login)
login_button.pack()
def open_employee_management(user):
root.deiconify()
display_employees()
if user[3] == 'admin':
add_button = ttk.Button(root, text="Add Employee", command=add_employee_window)
add_button.pack()
delete_button = ttk.Button(root, text="Delete Employee", command=delete_employee_window)
delete_button.pack()
update_button = ttk.Button(root, text="Update Employee", command=update_employee_window)
update_button.pack()
def add_employee_window():
add_window = tk.Toplevel(root)
add_window.title("Add Employee")
name_label = ttk.Label(add_window, text="Name:")
name_label.pack()
name_entry = ttk.Entry(add_window)
name_entry.pack()
position_label = ttk.Label(add_window, text="Position:")
position_label.pack()
position_entry = ttk.Entry(add_window)
position_entry.pack()
department_label = ttk.Label(add_window, text="Department:")
department_label.pack()
department_entry = ttk.Entry(add_window)
department_entry.pack()
def save_employee():
name = name_entry.get()
position = position_entry.get()
department = department_entry.get()
insert_employee(name, position, department)
add_window.destroy()
display_employees()
save_button = ttk.Button(add_window, text="Save", command=save_employee)
save_button.pack()
def delete_employee_window():
delete_window = tk.Toplevel(root)
delete_window.title("Delete Employee")
id_label = ttk.Label(delete_window, text="Employee ID:")
id_label.pack()
id_entry = ttk.Entry(delete_window)
id_entry.pack()
def delete_employee_record():
id = id_entry.get()
delete_employee(id)
delete_window.destroy()
display_employees()
delete_button = ttk.Button(delete_window, text="Delete", command=delete_employee_record)
delete_button.pack()
def update_employee_window():
update_window = tk.Toplevel(root)
update_window.title("Update Employee")
id_label = ttk.Label(update_window, text="Employee ID:")
id_label.pack()
id_entry = ttk.Entry(update_window)
id_entry.pack()
name_label = ttk.Label(update_window, text="Name:")
name_label.pack()
name_entry = ttk.Entry(update_window)
name_entry.pack()
position_label = ttk.Label(update_window, text="Position:")
position_label.pack()
position_entry = ttk.Entry(update_window)
position_entry.pack()
department_label = ttk.Label(update_window, text="Department:")
department_label.pack()
department_entry = ttk.Entry(update_window)
department_entry.pack()
def update_employee_record():
id = id_entry.get()
name = name_entry.get()
position = position_entry.get()
department = department_entry.get()
update_employee(id, name, position, department)
update_window.destroy()
display_employees()
update_button = ttk.Button(update_window, text="Update", command=update_employee_record)
update_button.pack()
root = tk.Tk()
root.title("Employee Management System")
root.withdraw()
create_employee_table()
create_user_table()
# 添加一个默认管理员用户
conn = sqlite3.connect('user.db')
c = conn.cursor()
c.execute("INSERT INTO users (username, password, role, permission) VALUES (?, ?, ?, ?)", ('admin', 'admin', 'admin', 'full'))
conn.commit()
conn.close()
tree = ttk.Treeview(root, columns=('Name', 'Position', 'Department'))
tree.heading('#0', text='ID')
tree.heading('Name', text='Name')
tree.heading('Position', text='Position')
tree.heading('Department', text='Department')
tree.pack()
login_window()
root.mainloop()
这个代码示例使用了tkinter.ttk
模块来创建GUI界面,其中的ttk.Label
、ttk.Entry
、ttk.Button
等部件都是ttk
模块提供的。同时,使用ttk.Treeview
来展示员工信息列表。
九、归纳总结
这个重写的员工管理信息系统程序具有以下功能:
-
用户登录功能:用户可以通过输入用户名和密码进行登录,系统会验证用户信息并根据用户角色打开相应的功能界面。程序会检查用户输入的用户名和密码是否在用户数据库中存在,如果存在且匹配,则允许用户登录。
-
员工信息展示功能:在用户成功登录后的主界面上展示所有员工的信息,包括员工的ID、姓名、职位和部门。这些信息会以表格的形式展示在程序的主界面上,方便用户查看。
-
添加员工功能:管理员用户可以通过点击“Add Employee”按钮打开添加员工界面,然后输入新员工的姓名、职位和部门信息,最后点击“Save”按钮将新员工信息保存到员工数据库中。添加员工功能可以帮助管理员快速录入新员工的信息。
-
删除员工功能:管理员用户可以通过点击“Delete Employee”按钮打开删除员工界面,然后输入要删除的员工ID,最后点击“Delete”按钮将对应员工信息从数据库中删除。删除员工功能可以帮助管理员管理员工信息,保持数据库的准确性。
-
更新员工功能:管理员用户可以通过点击“Update Employee”按钮打开更新员工界面,然后输入要更新的员工ID以及新的姓名、职位和部门信息,最后点击“Update”按钮将对应员工信息更新到数据库中。更新员工功能可以帮助管理员及时更新员工信息。
-
创建员工和用户数据库表格功能:程序在启动时会创建员工和用户两个数据库表格,用于存储员工信息和用户信息。这样可以保证程序有一个持久化的数据存储方式,方便用户管理员工和用户信息。
-
默认管理员用户创建功能:程序在启动时会向用户数据库中添加一个默认的管理员用户,用户名为“admin”,密码为“admin”,角色为“admin”,拥有完整的权限。这样可以确保系统有一个管理员用户可以管理系统。
总的来说,这个员工管理信息系统程序具有用户登录、员工信息展示、添加员工、删除员工、更新员工、创建数据库表格和默认管理员用户创建等功能,可以帮助管理员高效地管理员工信息。另外,tkinter.ttk模块的使用实现了程序漂亮美观时尚现代的图形化界面。希望以上详细的功能描述能够帮助您更好地理解这个程序。当然,实际开发当中还要添加更多功能,完善事件处理,做进一步优化,才能满足客户需求。