#使用说明:
1,桌面上有71班学生名单.xlsx和72班学生名单.xlsx或者按照需求,放您想要的两个文件
2,修改代码内部文件名称
3,pyinstalLer库压缩
4点开压缩后的exe文件运行
#使用前先下载pyinstall库压缩,压缩的代码
pyinstall -w -F -i "图片的绝对路径" "python的绝对路径"
压缩后寻找dist文件中的python文件名.py
打开exe文件即可
系统响应预计在1分钟
import os
import openpyxl
import tkinter as tk
import random
def on_resize(event):
"""当窗口大小改变时,调整文本大小"""
# 计算新的字体大小
new_font_size = int(min(event.width / 3.6, event.height / 3.6))
label.config(font=('Arial', new_font_size, 'bold'))
# 创建主窗口
root = tk.Tk()
root.configure(background='white')
root.geometry("500x500+300+100")
root.title('')
# 创建一个 Label
label = tk.Label(root, text="请选择班级", bg="black", fg="yellow")
# 绑定窗口大小变化事件
root.bind("<Configure>", on_resize)
# 使用 place 布局管理器
label.place(
anchor='n',
relx=0.5, rely=0.16, # 控件相对于父容器的相对位置 x=0.5, y=0.16
relwidth=0.8, relheight=0.21
)
#
def iom():
# 获取桌面路径
desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')
# 指定文件路径
file_path1 = os.path.join(desktop_path, '71班学生名单.xlsx')
wb = openpyxl.load_workbook(file_path1)
sheet2 = wb['Sheet1']
list2 = [] # 存放姓名
# 读取其他列也可以:将以第二列为例,min_col=2, max_col=2即可,其他类比
# 获取第三列的数据
for row in sheet2.iter_rows(min_row=1, max_row=sheet2.max_row, min_col=1, max_col=1):
for cell in row:
list2.append(cell.value)
root = tk.Tk()
#这顶窗口背景色
root.configure(background='white')
# 设置窗口大小和位置
root.geometry("500x500+300+100")
#放进去text文本 (幸运儿)
#设置label控件的大小
label=tk.Label(root)
# 设置标签的宽度和高度
label.configure(width=400, height=320)
#加上文本
label.configure(text="71班幸运学生")
# 更改标签本身的背景颜色
label.configure(bg="white")
def change_text_color(label):
"""改变标签文字的颜色,并安排下一次颜色更改的时间"""
colors = ['blue', 'yellow', 'orange',
'purple', 'pink', 'cyan', 'magenta', 'lime',
'teal', 'navy', 'aqua', 'maroon', 'olive',
'silver', 'gray', 'black', 'white', '#FFC0CB']
color = random.choice(colors)
label.config(fg=color) # 改变颜色
stime = [500, 750, 1000, 1250, 1500, 1750, 2000]
Ti_me = random.choice(stime)
# 使用lambda表达式来确保正确调用函数
root.after(Ti_me, lambda: change_text_color(label))
# 更改文本的颜色
change_text_color(label)
# 使用 place 布局管理器
label.place(
anchor='n'
, relx=0.5, rely=0.16 # 控件相对于父容器的相对位置x=0,y=0
, relwidth=0.8, relheight=0.21
)
#绑定输入的列表,从列表中随机抽一个点到名的人的(labal的字体)
label1 = tk.Label(root)
# 更改标签本身的背景颜色
label1.configure(bg="white")
#加上边框和边框颜色
label1.configure(borderwidth=3,relief='solid')
# 更改文本
label1.configure(text='')
# 更改文本的颜色
label1.configure(fg="red")
# 使用 place 布局管理器
label1.place(
anchor='center'
, relx=0.5, rely=0.52 # 控件相对于父容器的相对位置x,y
, relwidth=0.47, relheight=0.18
)
#给一个姓名列表,随机返回一个列表中的值
def rc():
label1.configure(text=random.choice(list2[1:])) # 更新标签内容
#点击按钮后随机选择一个人名,并且让labal中的文本换为随机选择出来的文本
button = tk.Button(root,
text="开始点名",
anchor='center', # 文本在小部件的位置(这里是中间)
activeforeground='black', # 当鼠标放在按钮上时,按钮的前景色(实际是文本的颜色)
bd=5, # 边框宽度5px
bg='blue', # 没有点击按钮时的背景色
command=lambda: rc(), # 用来执行按钮关联的回调函数。当按钮被点击时,执行该函数。
fg='white', # 文本的颜色
font=('Arial', 10, 'bold'), # 按钮的字体样式
height=15, # 按钮的总高度
pady=20, # 按钮内的文本与上下边界的垂直间距为 20 像素
state='normal' # 设置按钮的可用状态
)
button.place(relx=0.5, rely=0.7,
relwidth=0.45, relheight=0.1,anchor='center')
labals=[label,label1]
# 更新控件和字体大小的函数,随着窗口大小而变化
def update_size(event, labels):
if not hasattr(update_size, 'updating'):
update_size.updating = True
new_font_size = int(min(event.width / 10, event.height / 10)) # 根据窗口的宽度和高度计算新的字体大小
for label in labels:
label.config(font=("Helvetica", new_font_size, "bold"))
root.after_idle(lambda: setattr(update_size, 'updating', False))
# 绑定窗口大小改变事件
root.bind("<Configure>", lambda event: update_size(event, labals))
root.mainloop() #进入循环
return label
def io1():
# 获取桌面路径
desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')
# 指定文件路径
file_path1 = os.path.join(desktop_path, '72班学生名单.xlsx')
wb = openpyxl.load_workbook(file_path1)
sheet2 = wb['Sheet1']
list2 = [] # 存放姓名
# 读取其他列也可以:将以第二列为例,min_col=2, max_col=2即可,其他类比
# 获取第三列的数据
for row in sheet2.iter_rows(min_row=1, max_row=sheet2.max_row, min_col=1, max_col=1):
for cell in row:
list2.append(cell.value)
root = tk.Tk()
#这顶窗口背景色
root.configure(background='white')
# 设置窗口大小和位置
root.geometry("500x500+300+100")
#放进去text文本 (幸运儿)
#设置label控件的大小
label=tk.Label(root)
# 设置标签的宽度和高度
label.configure(width=400, height=320)
#加上文本
label.configure(text="72班幸运学生")
# 更改标签本身的背景颜色
label.configure(bg="white")
def change_text_color(label):
"""改变标签文字的颜色,并安排下一次颜色更改的时间"""
colors = ['blue', 'yellow', 'orange',
'purple', 'pink', 'cyan', 'magenta', 'lime',
'teal', 'navy', 'aqua', 'maroon', 'olive',
'silver', 'gray', 'black', 'white', '#FFC0CB']
color = random.choice(colors)
label.config(fg=color) # 改变颜色
stime = [500, 750, 1000, 1250, 1500, 1750, 2000]
Ti_me = random.choice(stime)
# 使用lambda表达式来确保正确调用函数
root.after(Ti_me, lambda: change_text_color(label))
# 更改文本的颜色
change_text_color(label)
# 使用 place 布局管理器
label.place(
anchor='n'
, relx=0.5, rely=0.16 # 控件相对于父容器的相对位置x=0,y=0
, relwidth=0.8, relheight=0.21
)
#绑定输入的列表,从列表中随机抽一个点到名的人的(labal的字体)
label1 = tk.Label(root)
# 更改标签本身的背景颜色
label1.configure(bg="white")
#加上边框和边框颜色
label1.configure(borderwidth=3,relief='solid')
# 更改文本
label1.configure(text='')
# 更改文本的颜色
label1.configure(fg="red")
# 使用 place 布局管理器
label1.place(
anchor='center'
, relx=0.5, rely=0.52 # 控件相对于父容器的相对位置x,y
, relwidth=0.47, relheight=0.18
)
#给一个姓名列表,随机返回一个列表中的值
def rc():
label1.configure(text=random.choice(list2[1:])) # 更新标签内容
#点击按钮后随机选择一个人名,并且让labal中的文本换为随机选择出来的文本
button = tk.Button(root,
text="开始点名",
anchor='center', # 文本在小部件的位置(这里是中间)
activeforeground='black', # 当鼠标放在按钮上时,按钮的前景色(实际是文本的颜色)
bd=5, # 边框宽度5px
bg='blue', # 没有点击按钮时的背景色
command=lambda: rc(), # 用来执行按钮关联的回调函数。当按钮被点击时,执行该函数。
fg='white', # 文本的颜色
font=('Arial', 10, 'bold'), # 按钮的字体样式
height=15, # 按钮的总高度
pady=20, # 按钮内的文本与上下边界的垂直间距为 20 像素
state='normal' # 设置按钮的可用状态
)
button.place(relx=0.5, rely=0.7,
relwidth=0.45, relheight=0.1,anchor='center')
labals=[label,label1]
# 更新控件和字体大小的函数,随着窗口大小而变化
def update_size(event, labels):
if not hasattr(update_size, 'updating'):
update_size.updating = True
new_font_size = int(min(event.width / 10, event.height / 10)) # 根据窗口的宽度和高度计算新的字体大小
for label in labels:
label.config(font=("Helvetica", new_font_size, "bold"))
root.after_idle(lambda: setattr(update_size, 'updating', False))
# 绑定窗口大小改变事件
root.bind("<Configure>", lambda event: update_size(event, labals))
root.mainloop() #进入循环
return label
#
#点中71班按钮后执行的函数
def seven1():
root.destroy()
iom()
#点中72班按钮后执行的函数
def seven2():
root.destroy()
io1()
#71班按钮
a = tk.Button(root,
text="71班",
anchor='center', # 文本在小部件的位置(这里是中间)
activeforeground='blue', # 当鼠标放在按钮上时,按钮的前景色(实际是文本的颜色)
bd=5, # 边框宽度5px
bg='black', # 没有点击按钮时的背景色
command=seven1, # 用来执行按钮关联的回调函数。当按钮被点击时,执行该函数。
fg='yellow', # 文本的颜色
font=('Arial', 32, 'bold'), # 按钮的字体样式
height=15, # 按钮的总高度
pady=20, # 按钮内的文本与上下边界的垂直间距为 20 像素
state='normal' # 设置按钮的可用状态
)
a.place(relx=0.103, rely=0.44,
relwidth=0.36, relheight=0.25)
a.bind("<Configure>", on_resize)
# #72班按钮
b = tk.Button(root,
text="72班",
anchor='center', # 文本在小部件的位置(这里是中间)
activeforeground='blue', # 当鼠标放在按钮上时,按钮的前景色(实际是文本的颜色)
bd=5, # 边框宽度5px
bg='black', # 没有点击按钮时的背景色
command=seven2, # 用来执行按钮关联的回调函数。当按钮被点击时,执行该函数。
fg='yellow', # 文本的颜色
font=('Arial', 32, 'bold'), # 按钮的字体样式
height=15, # 按钮的总高度
pady=20, # 按钮内的文本与上下边界的垂直间距为 20 像素
state='normal' # 设置按钮的可用状态
)
b.place(relx=0.511, rely=0.44,
relwidth=0.38, relheight=0.25)
b.bind("<Configure>", on_resize)
# 启动事件循环
root.mainloop()
点名2.0版本
我不是立达刘宁宇2024-08-28 10:03
相关推荐
Agent世界15 分钟前
用 CDP 做发布自动化:四道机器闸门与产物验收FungLeo15 分钟前
成为全栈·Vue 管理后台篇·编辑器图片上传:从文件选择到文章附件关联2601_9679873724 分钟前
Sqlite参考手册某哈压力大34 分钟前
SSH 突然 Connection reset?不是密钥坏了,是 OpenSSL 把 sshd 干趴了程序员大阳36 分钟前
副队长ES6+教程(3)--解构赋值神膘护体小月半1 小时前
css - 元素背景图由 hover 触发,初次加载时闪动的解决办法用户019027581611 小时前
A 股到价提醒机器人:AlphaFeed + 飞书/钉钉 Webhook程序员大阳1 小时前
副队长ES6+教程(2)--let 与 const特立独行的猫A1 小时前
Ionic + 仓颉语言的 cj-tauri 开发框架实战:开发精美移动端 APPdeli0071 小时前
写满 200GB 后 SSD 为什么掉到 95MB/s?SLC 缓存模型实测