6 Commits

Author SHA1 Message Date
69f320ec2d fix icon not packed within EXE file 2024-06-22 22:27:54 +08:00
594e3e87fe bump version to v1.1 2024-06-22 22:02:36 +08:00
1acd1b7fd4 fix text 2024-06-22 21:54:28 +08:00
2100d63d64 add jumping number when drawing 2024-06-22 21:52:57 +08:00
ae467e072a set icon for widget 2024-06-22 13:55:47 +08:00
044f40ec78 update some font size 2024-06-07 19:50:08 +08:00
2 changed files with 109 additions and 23 deletions

65
auto-py-to-exe.json Normal file
View File

@ -0,0 +1,65 @@
{
"version": "auto-py-to-exe-configuration_v1",
"pyinstallerOptions": [
{
"optionDest": "noconfirm",
"value": true
},
{
"optionDest": "filenames",
"value": "J:/Projects/LuckyDraw/main.py"
},
{
"optionDest": "onefile",
"value": true
},
{
"optionDest": "console",
"value": false
},
{
"optionDest": "icon_file",
"value": "J:/Projects/LuckyDraw/icon.ico"
},
{
"optionDest": "clean_build",
"value": false
},
{
"optionDest": "strip",
"value": false
},
{
"optionDest": "noupx",
"value": false
},
{
"optionDest": "disable_windowed_traceback",
"value": false
},
{
"optionDest": "uac_admin",
"value": false
},
{
"optionDest": "uac_uiaccess",
"value": false
},
{
"optionDest": "argv_emulation",
"value": false
},
{
"optionDest": "bootloader_ignore_signals",
"value": false
},
{
"optionDest": "datas",
"value": "J:/Projects/LuckyDraw/icon.png;."
}
],
"nonPyinstallerOptions": {
"increaseRecursionLimit": true,
"manualArguments": ""
}
}

67
main.py
View File

@ -3,6 +3,9 @@ from tkinter import simpledialog, messagebox
import random
import winreg
from datetime import datetime
import os
import pkg_resources
import base64
# 注册表路径
REGISTRY_PATH = r"Software\StudentIDDraw"
@ -11,9 +14,11 @@ REGISTRY_PATH = r"Software\StudentIDDraw"
DEFAULT_ALLOW_REPEAT = False
DEFAULT_MIN_ID = 1
DEFAULT_MAX_ID = 45
PASSWORD = "admin123"
PASSWORD = "Admin@123"
selected_ids = []
is_running = False
job = None
# 注册表工具函数
@ -110,7 +115,6 @@ def generate_random_id(selected_ids, allow_repeat, min_id, max_id):
else:
return random.randint(min_id, max_id)
# 更新历史记录列表
def update_history_list():
global history_list
@ -119,22 +123,35 @@ def update_history_list():
for id, timestamp in selected_ids:
history_list.insert(tk.END, f"{id} ({timestamp})")
# 每隔 5 毫秒更新一次显示的随机学号
def update_number(allow_repeat, min_id, max_id):
global job
if is_running:
random_id = generate_random_id(selected_ids, allow_repeat, min_id, max_id)
label.config(text=str(random_id))
job = root.after(5, update_number, allow_repeat, min_id, max_id)
# 抽取学号按钮点击事件处理函数
def draw_student_id():
global selected_ids
load_selected_ids()
global is_running, job, selected_ids
allow_repeat, min_id, max_id = load_settings()
id = generate_random_id(selected_ids, allow_repeat, min_id, max_id)
if id is not None:
if is_running:
# 停止跳动,保存最终学号
root.after_cancel(job)
id = label.cget("text")
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
label.config(text=str(id), font=("宋体", 50))
selected_ids.append((str(id), timestamp))
save_selected_ids()
if settings_window:
update_history_list()
button.config(text="开始")
is_running = False
else:
label.config(text="已抽完所有学号", font=("宋体", 30))
# 开始跳动
is_running = True
button.config(text="停止")
update_number(allow_repeat, min_id, max_id)
# 设置按钮点击事件处理函数
@ -155,10 +172,6 @@ def open_settings():
settings_window.grid_columnconfigure(1, weight=1)
settings_window.grid_rowconfigure(4, weight=1)
tk.Label(settings_window, text="历史记录:", font=("宋体", 12)).grid(
row=0, column=0, padx=5, pady=5, sticky="w"
)
load_selected_ids()
history_frame = tk.Frame(settings_window)
@ -232,26 +245,26 @@ def open_settings():
selected_ids = []
update_history_list()
tk.Button(settings_window, text="新增记录", font=("宋体", 12), command=add_id).grid(
tk.Button(settings_window, text="新增记录", font=("宋体", 10), command=add_id).grid(
row=2, column=0, padx=5, pady=5, sticky="ew"
)
tk.Button(settings_window, text="删除记录", font=("宋体", 12), command=delete_id).grid(
tk.Button(settings_window, text="删除记录", font=("宋体", 10), command=delete_id).grid(
row=2, column=1, padx=5, pady=5, sticky="ew"
)
tk.Button(settings_window, text="修改记录", font=("宋体", 12), command=edit_id).grid(
tk.Button(settings_window, text="修改记录", font=("宋体", 10), command=edit_id).grid(
row=3, column=0, padx=5, pady=5, sticky="ew"
)
tk.Button(
settings_window, text="清空记录", font=("宋体", 12), command=clear_all_ids
settings_window, text="清空记录", font=("宋体", 10), command=clear_all_ids
).grid(row=3, column=1, padx=5, pady=5, sticky="ew")
allow_repeat_var = tk.BooleanVar(value=allow_repeat)
tk.Checkbutton(
settings_window, text="允许重复抽号", font=("宋体", 12), variable=allow_repeat_var
settings_window, text="允许重复抽号", font=("宋体", 10), variable=allow_repeat_var
).grid(row=5, column=0, padx=5, pady=5, sticky="w")
tk.Label(settings_window, text="学号范围:", font=("宋体", 12)).grid(
row=6, column=0, padx=5, pady=5, sticky="w"
tk.Label(settings_window, text="学号范围:", font=("宋体", 10)).grid(
row=6, column=0, padx=5, pady=5, sticky="e"
)
min_id_var = tk.StringVar(value=min_id)
max_id_var = tk.StringVar(value=max_id)
@ -274,7 +287,7 @@ def open_settings():
else:
messagebox.showerror("错误", "密码错误")
tk.Button(settings_window, text="保存", font=("宋体", 12), command=save_changes).grid(
tk.Button(settings_window, text="保存", font=("宋体", 10), command=save_changes).grid(
row=8, column=0, columnspan=2, padx=5, pady=20, sticky="ew"
)
@ -294,23 +307,31 @@ def close_settings_window():
settings_window.destroy()
settings_window = None
def get_icon_data():
icon_path = pkg_resources.resource_filename(__name__, 'icon.png')
with open(icon_path, 'rb') as icon_file:
return base64.b64encode(icon_file.read())
# 初始化全局变量
selected_ids = load_selected_ids()
settings_window = None
history_list = None
icon_data = get_icon_data()
# 创建主窗口
root = tk.Tk()
root.title("随机抽号机")
root.attributes("-topmost", 1) # 置顶显示
root.attributes("-topmost", 1)
icon_image = tk.PhotoImage(data=icon_data)
root.iconphoto(True, icon_image)
# 创建标签,用于显示抽取的学号
label = tk.Label(root, text="", font=("宋体", 50))
label.pack(pady=20, padx=20)
# 创建抽取学号按钮
button = tk.Button(root, text="抽取学号", font=("宋体", 12), command=draw_student_id)
button = tk.Button(root, text="开始", font=("宋体", 12), command=draw_student_id)
button.pack(pady=10)
# 创建设置按钮
@ -320,7 +341,7 @@ settings_button.pack(pady=10)
# 创建多行标签,用于显示信息,内容居中显示
info_label = tk.Label(
root,
text="v1.0 2024/06/07\nCopyright 2024 earthjasonlin\n保留所有权利。\n本程序仅作为交流学习使用",
text="LuckyDraw v1.1 2024/06/22\nCopyright 2024 earthjasonlin\n保留所有权利。\n本程序仅作为交流学习使用",
font=("宋体", 10),
justify="center",
anchor="center",