基于Python Tkinter与SQL Server的积分管理系统技术问询
积分管理系统核心功能实现伪代码(Tkinter + SQL Server)
1. 登录模块:获取用户名与部门信息
该模块负责用户身份验证,验证通过后提取用户所属部门并保存当前用户信息,为后续积分计算和存储提供基础数据。
import tkinter as tk import pyodbc from tkinter import messagebox # 存储当前登录用户信息(新手入门可先用全局变量,后续可优化为类属性) current_user = {"username": "", "department": ""} def login(): username = entry_username.get().strip() password = entry_password.get().strip() # 连接SQL Server(替换为你的数据库配置) try: conn = pyodbc.connect( "Driver={SQL Server};" "Server=你的服务器地址;" "Database=你的数据库名;" "UID=数据库登录账号;" "PWD=数据库登录密码;" ) cursor = conn.cursor() # 从用户表查询部门信息(假设用户表名为user_info) cursor.execute( "SELECT department FROM user_info WHERE username = ? AND password = ?", (username, password) ) result = cursor.fetchone() if result: current_user["username"] = username current_user["department"] = result[0] login_window.destroy() open_main_form() else: label_error.config(text="用户名或密码错误") cursor.close() conn.close() except Exception as e: messagebox.showerror("错误", f"数据库连接失败:{str(e)}") # 构建登录窗口 login_window = tk.Tk() login_window.title("用户登录") login_window.geometry("300x200") # 用户名输入区域 tk.Label(login_window, text="用户名").pack(pady=5) entry_username = tk.Entry(login_window, width=30) entry_username.pack(pady=5) # 密码输入区域 tk.Label(login_window, text="密码").pack(pady=5) entry_password = tk.Entry(login_window, width=30, show="*") entry_password.pack(pady=5) # 错误提示 label_error = tk.Label(login_window, text="", fg="red") label_error.pack(pady=5) # 登录按钮 tk.Button(login_window, text="登录", command=login, width=15).pack(pady=10) login_window.mainloop()
2. 主表单与积分计算、存储模块
该模块展示提交表单,根据用户的Yes/No选择实时计算积分,并将结果存储到SQL Server数据库中。
def open_main_form(): main_window = tk.Tk() main_window.title(f"积分提交 - {current_user['department']}") main_window.geometry("400x500") # 积分规则配置(对应你的业务逻辑表,可根据需求修改) score_rules = { "是否完成每日任务": {"Yes": 10, "No": 0}, "是否参加部门培训": {"Yes": 20, "No": 0}, "是否提交周报": {"Yes": 15, "No": 0} # 添加更多问题及对应积分规则 } # 存储用户的选择结果 user_choices = {} # 动态生成表单问题 for idx, question in enumerate(score_rules.keys()): frame = tk.Frame(main_window) frame.pack(pady=8, fill="x", padx=20) tk.Label(frame, text=f"{idx+1}. {question}").pack(anchor="w") # 单选按钮绑定变量 choice_var = tk.StringVar(value="No") user_choices[question] = choice_var tk.Radiobutton(frame, text="Yes", variable=choice_var, value="Yes").pack(side="left", padx=10) tk.Radiobutton(frame, text="No", variable=choice_var, value="No").pack(side="left") def calculate_and_save(): total_score = 0 # 计算总积分 for question, var in user_choices.items(): total_score += score_rules[question][var.get()] # 存储积分到数据库(假设积分表名为user_scores) try: conn = pyodbc.connect( "Driver={SQL Server};" "Server=你的服务器地址;" "Database=你的数据库名;" "UID=数据库登录账号;" "PWD=数据库登录密码;" ) cursor = conn.cursor() # 参数化插入,避免SQL注入 cursor.execute(""" INSERT INTO user_scores (username, department, total_score, submit_time) VALUES (?, ?, ?, GETDATE()) """, (current_user["username"], current_user["department"], total_score)) conn.commit() messagebox.showinfo("成功", f"积分提交完成,本次积分:{total_score}") cursor.close() conn.close() except Exception as e: messagebox.showerror("错误", f"存储失败:{str(e)}") # 提交按钮 tk.Button(main_window, text="提交积分", command=calculate_and_save, width=15).pack(pady=20) main_window.mainloop()
3. 数据库表结构参考
用户信息表(user_info)
CREATE TABLE user_info ( username VARCHAR(50) PRIMARY KEY, password VARCHAR(50) NOT NULL, department VARCHAR(50) NOT NULL )
积分记录表(user_scores)
CREATE TABLE user_scores ( id INT IDENTITY(1,1) PRIMARY KEY, username VARCHAR(50) NOT NULL, department VARCHAR(50) NOT NULL, total_score INT NOT NULL, submit_time DATETIME NOT NULL )
内容的提问来源于stack exchange,提问作者Anindya Gupta
相关产品推荐
相关产品推荐

