基于Python怎么编写一个点名器
主界面
定义主界面。使用“w+”模式创建test.txt文件(我添加了个背景图片,若不需要可省略)
#打开时预加载储存在test.txt文件中的花名册namelist = []
with open("
test.txt"
, "
r"
) as f:
for line in f.readlines():
line = line.strip('
\n'
)
namelist.append(line)
win = Tk()
win.title('
小白点名器'
)
win.geometry('
500x300'
)
#定义画布,添加背景图片
canvas = Canvas(win,width=500,height=300)
img_obj = PhotoImage(file=r"
C:\Users\ge\Downloads\IMG_202206307919_png.png"
) #需输入照片路径
image = canvas.create_image(250,0,anchor = "
n"
, image = img_obj)
canvas.pack()
a = StringVar()
b = StringVar()
b.set('
开始'
)
#定义可变文本信息
Label1 = Label(win, textvariable=a, font=('
黑体'
, 100)).place(y= 60 , x=65)
#定义四个按钮
Button1 = Button(win, textvariable=b, font=('
等线'
, 30), command = zhuanzhuan).place(y=210,x = 190)
Button2 = Button(win, text = '
添加姓名'
, font=('
等线'
, 20), command = addname).place(x= 50,y =0)
Button3 = Button(win, text = '
查看'
, font=('
等线'
, 20), command = chakan).place(x= 230,y =0)
Button4 = Button(win, text = '
指南'
, font=('
等线'
, 20), command = zhinan).place(x= 360,y =0)
win.mainloop() 添加姓名
定义添加姓名界面,每添加一次姓名就保存到test.txt文件中,判断输入是否为空(添加提示框)、判断花名册是否为空。
#定义添加姓名界面def addname():
global Entry1
window = Tk()
window.title('
姓名添加器'
)
window.geometry('
400x200+500+200'
)
Label11 = Label(window, text = '
请在下方输入你要添加的姓名'
, font=('
黑体'
, 18), anchor='
center'
).place(y=30, x=25)
Entry1 = Entry(window, font=('
等线'
, 30), width=70)
Entry1.place(y=80, x=70, width=200, height=80)
Button3 = Button(window, text = '
确认'
, font=('
等线'
, 18), command = addname1).place(x= 300,y =80, height=80)
#每添加一次姓名就保存到test.txt文件中
def addname1():
global namelist #声明为全局变量实时更新
if len(Entry1.get()) == 0:
tkinter.messagebox.showinfo('
提示'
, '
姓名输入不能为空哦'
)
else:
if len(Entry1.get()) == 2:
zhongjian = list(Entry1.get())[::1]
zhongjian1 = zhongjian[0] + '
'
+zhongjian[1]
if len(namelist) == 0:
nam = zhongjian1
else:
nam = '
\n'
+ zhongjian1
else:
if len(namelist) == 0:
nam = str(Entry1.get())
else:
nam = '
\n'
+ str(Entry1.get())
with open("
test.txt"
, "
a"
) as f:
f.write(nam)
tip = '
姓名:'
+ Entry1.get() + '
添加成功'
tkinter.messagebox.showinfo('
提示'
, tip)
print(nam)
namelist = []
with open("
test.txt"
, "
r"
) as f:
for line in f.readlines():
line = line.strip('
\n'
)
namelist.append(line) 查看花名册
这个比较简单,使用Text来显示字典内的信息即可
def chakan():window = Tk()
window.title('
花名册查看'
)
window.geometry('
350x200+500+200'
)
console = Text(window, font=('
等线'
, 11))
console.place(y=20, x=35, width=280, height=170)
console.insert(1.0,namelist) 使用指南
同上,使用Text显示
def zhinan():window = Tk()
window.title('
小白点名器使用指南'
)
window.geometry('
350x230+500+200'
)
console = Text(window, font=('
等线'
, 11))
console.place(y=20, x=35, width=280, height=190)
console.insert(1.0, '
欢迎使用小白点名器1.0\n你可以在”添加姓名按钮上输入你要添加的名字\n你可以在”查看“按钮中查看花名册中所有的名字'
'
\n你可以在此程序同级的名为”花名册.txt“的文件夹中直接批量添加、删减姓名(使用回车做分隔)\n--------------------------------\n'
'
(指南之外)此程序在CSDN中已开源,欢迎访问我的博客:晋升阁\n需要合作的可加我微信:baijinge1137'
) 名字转动功能
判断“开始”、“停止”状态。定义线程。启用一个线程
#判断状态def zhuanzhuan():
if b.get() == '
开始'
:
b.set('
停止'
)
elif b.get() =="
停止"
:
b.set('
开始'
)
_thread.start_new_thread(xiancheng,()) #启用一个线程来转动姓名
#定义一个线程
def xiancheng():
global xuanzhong
while b.get()=='
停止'
:
try:
xuanzhong = random.choice(namelist)
a.set(xuanzhong)
Label1.updata()#刷新数据
time.sleep(0.3)#0.3秒刷新一次
except:
continue
time.sleep(0.3)
a.set(xuanzhong) 完整代码
如果不需要在主界面添加背景图片,可以删除代码的第90到94行。若是需要添加背景图片的需注意路径地址是否正确
import randomimport time
from tkinter import *
import _thread
import tkinter.messagebox
def zhuanzhuan():
if b.get() == '
开始'
:
b.set('
停止'
)
elif b.get() =="
停止"
:
b.set('
开始'
)
_thread.start_new_thread(xiancheng,()) #启用一个线程来转动姓名
def xiancheng():
global xuanzhong
while b.get()=='
停止'
:
try:
xuanzhong = random.choice(namelist)
a.set(xuanzhong)
Label1.updata()
time.sleep(0.3)
except:
continue
time.sleep(0.3)
a.set(xuanzhong)
def addname1():
global namelist #声明为全局变量实时更新
if len(Entry1.get()) == 0:
tkinter.messagebox.showinfo('
提示'
, '
姓名输入不能为空哦'
)
else:
if len(Entry1.get()) == 2:
zhongjian = list(Entry1.get())[::1]
zhongjian1 = zhongjian[0] + '
'
+zhongjian[1]
if len(namelist) == 0:
nam = zhongjian1
else:
nam = '
\n'
+ zhongjian1
else:
if len(namelist) == 0:
nam = str(Entry1.get())
else:
nam = '
\n'
+ str(Entry1.get())
with open("
test.txt"
, "
a"
) as f:
f.write(nam)
tip = '
姓名:'
+ Entry1.get() + '
添加成功'
tkinter.messagebox.showinfo('
提示'
, tip)
print(nam)
namelist = []
with open("
test.txt"
, "
r"
) as f:
for line in f.readlines():
line = line.strip('
\n'
)
namelist.append(line)
def chakan():
window = Tk()
window.title('
花名册查看'
)
window.geometry('
350x200+500+200'
)
console = Text(window, font=('
等线'
, 11))
console.place(y=20, x=35, width=280, height=170)
console.insert(1.0,namelist)
def zhinan():
window = Tk()
window.title('
小白点名器使用指南'
)
window.geometry('
350x230+500+200'
)
console = Text(window, font=('
等线'
, 11))
console.place(y=20, x=35, width=280, height=190)
console.insert(1.0, '
欢迎使用小白点名器1.0\n你可以在”添加姓名按钮上输入你要添加的名字\n你可以在”查看“按钮中查看花名册中所有的名字'
'
\n你可以在此程序同级的名为”花名册.txt“的文件夹中直接批量添加、删减姓名(使用回车做分隔)\n--------------------------------\n'
'
(指南之外)此程序在CSDN中已开源,欢迎访问我的博客:晋升阁\n需要合作的可加我微信:baijinge1137'
)
def addname():
global Entry1
window = Tk()
window.title('
姓名添加器'
)
window.geometry('
400x200+500+200'
)
Label11 = Label(window, text = '
请在下方输入你要添加的姓名'
, font=('
黑体'
, 18), anchor='
center'
).place(y=30, x=25)
Entry1 = Entry(window, font=('
等线'
, 30), width=70)
Entry1.place(y=80, x=70, width=200, height=80)
Button3 = Button(window, text = '
确认'
, font=('
等线'
, 18), command = addname1).place(x= 300,y =80, height=80)
namelist = []
with open("
test.txt"
, "
r"
) as f:
for line in f.readlines():
line = line.strip('
\n'
)
namelist.append(line)
win = Tk()
win.title('
小白点名器'
)
win.geometry('
500x300'
)
canvas = Canvas(win,width=500,height=300)
img_obj = PhotoImage(file=r"
C:\Users\ge\Downloads\IMG_202206307919_png.png"
) #背景图片路径,若不需要添加将85—88行删掉即可
image = canvas.create_image(250,0,anchor = "
n"
, image = img_obj)
canvas.pack()
a = StringVar()
b = StringVar()
b.set('
开始'
)
Label1 = Label(win, textvariable=a, font=('
黑体'
, 100)).place(y= 60 , x=65)
Button1 = Button(win, textvariable=b, font=('
等线'
, 30), command = zhuanzhuan).place(y=210,x = 190)
Button2 = Button(win, text = '
添加姓名'
, font=('
等线'
, 20), command = addname).place(x= 50,y =0)
Button3 = Button(win, text = '
查看'
, font=('
等线'
, 20), command = chakan).place(x= 230,y =0)
Button4 = Button(win, text = '
指南'
, font=('
等线'
, 20), command = zhinan).place(x= 360,y =0)
win.mainloop()
Python是一种非常流行的编程语言,可以用它编写各种功能强大的应用程序。在学校教育中,点名是老师们必须完成的一项任务。本文将简要介绍如何使用Python编写一个高效的点名器。
1. 安装Python环境与IDE
要编写Python程序,首先需要安装Python运行环境和一个Python集成开发环境(IDE)。目前最流行的IDE包括PyCharm、Jupyter等。选择一个适合自己的IDE,配置好环境就可以开始写代码了。
2. 调用随机数模块
本点名器使用Python内置的random模块来获取随机数。在程序中使用import random语句即可调用random模块。
3. 编写点名器主程序
主程序采用while循环来实现重复点名的功能。程序可以加入判断语句确保同一个学生不会被重复点名。
4. 运行程序
将代码保存在一个.py文件中,并在Python环境中运行程序即可开始使用。程序支持自定义学生名单,只需要在代码中添加需要参与点名的学生名单。
Python是一门易学易用的编程语言,以其简单易懂的语法吸引了很多用户。本文介绍了如何使用Python编写一个点名器,可以方便老师们在教学中使用。此外,Python还可以用于数据分析、机器学习等领域。对于对Python语言感兴趣的人来说,这是一个好的入门教程。