一、概况
我们的三维世界并不能像四维世界那样,时间是可以随意控制的。时间过去就真的过去了,不得不说句时间过的有点快啊,高中一些片段的记忆还有时会浮现在脑海中。在高中课堂上,有的同学希望老师多提问一下自己,而有的同学在底下却默默祈祷,希望别提问他,甚至有的同学认为老师提问他是跟他过不去。现在回头想想,不知道会不会骂自己一句"鲨掉"。 那我们今天用Python程序写一个课堂点名器,用到的是Python中的GUI编程,这个真的很公平了,不偏不向。最后跟大家说句话:请珍惜时间。
Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python
二、页面搭建
我们用的是Python中的Tkinter模块,我们大概做的功能有三个:
- 随机点名
- 惩罚
- 日志写入
首先我们先简单的把界面搭建出来,代码很简单:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import tkinter as tk
class LoveYou():
初始化
def init(self):
第1步,建立窗口window
self.window = tk.Tk()
第2步,给窗口的可视化起名字
self.window.title('班级考单词程序')
第3步,设定窗口的大小(长*宽)
self.window.geometry('600x400')
self.text = tk.StringVar() # 创建str类型
self.count = tk.StringVar()
def take(self):
'''
负责随机抽取同学提问
:return:
'''
pass
def kill(self):
'''
负责根据星期几选择不同惩罚遍数
:return:
'''
pass
def main(self):
'''
主函数负责绘制
:return:
'''
绘制筛选信息
l2 = tk.Label(self.window, fg='red', textvariable=self.text, width=500, height=3)
l2.config(font='Helvetica -%d bold' % 30)
l2.pack()
绘制惩罚信息
l3 = tk.Label(self.window, fg='red', textvariable=self.count, width=500, height=3)
l3.config(font='Helvetica -%d bold' % 20)
l3.pack()
绘制筛选按钮
btntake = tk.Button(self.window, text="筛选", width=15, height=2, command=self.take)
btntake.pack()
绘制惩罚按钮
btnkill = tk.Button(self.window, text="惩罚", width=15, height=2, command=self.kill)
btnkill.pack()
进入循环
self.window.mainloop()
if name == 'main':
loveyou = LoveYou()
loveyou.main()
</pre>
<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
image
我们点击筛选按钮的时候就会执行take函数,此函数主要负责随机抽取同学。当我们点击惩罚按钮的时候就会执行kill函数 ,此函数主要负责惩罚同学。三、数据获取
在班级我们一般都会有个班级人员名单(xxx.xlsx)。当我们初始化程序的时候,我们把本地的数据读取到程序中。我们在本地创建一个表格写点假数据:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558768067778" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
image
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">通过xlrd模块可以把数据读取到程序中,但是我们需要安装这个模块:pip install xlrd
</pre>
我们写个函数读取:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def read_data(self):
'''
数据读取
:return:
'''
workbook = xlrd.open_workbook('1801.xlsx')
sheet1 = workbook.sheet_by_index(0) # sheet索引从0开始
data = sheet1.col_values(0) # 读取第一列内容
['表格 1', '姓名', '小王', '老王', '小明', '小红', '小绿', '小青', '小陈', '小赵', '小王', '小李', '小孙', '小周', '小吴', '小钱', '小史', '小三', '小四', '小五', '小六']
data.pop(0) # 把表格 1去掉
data.pop(0) # 把姓名 去掉
return data
</pre>
这个函数中返回的data就是从本地读取的同学名单。
四、点名实现
我们一共设定随机次数,例如50次,直到最后随机出来的同学才当做结果。大概逻辑如下:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def take(self):
'''
负责随机抽取同学提问
:return:
'''
for s in range(50):
'''
后几秒慢点,制造紧张气氛
'''
desc = ''
if s == 47:
time.sleep(0.5)
elif s == 48:
time.sleep(0.6)
elif s == 48:
time.sleep(0.7)
elif s == 49:
time.sleep(0.9)
else:
time.sleep(0.1)
classes = random.sample(self.data, 2)
desc += "呦,你被上帝选中了:%s\n" % classes[0]
desc += "呦,你看着也很不错呀:%s\n" % classes[1]
self.text.set(desc) # 设置内容
self.window.update() # 屏幕更新
</pre>
下面是动态图展示:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558768067789" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
image
五、惩罚实现
我们根据周一到周五,每天惩罚的量不一样,惩罚的方式暂定抄写吧。
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def kill(self):
'''
负责根据星期几选择不同惩罚遍数
:return:
'''
if self.day == 1:
count = random.randint(50, 100)
kill_desc = "上帝奖励了你们组%d遍" % (count)
elif self.day == 2:
count = random.randint(50, 120)
kill_desc = "上帝奖励了你们组%d遍" % (count)
self.count.set(kill_desc)
elif self.day == 3:
count = random.randint(50, 140)
kill_desc = "上帝奖励了你们组%d遍" % (count)
elif self.day == 4:
count = random.randint(50, 160)
kill_desc = "上帝奖励了你们组%d遍" % (count)
self.count.set(kill_desc)
elif self.day == 5:
count = random.randint(50, 180)
kill_desc = "上帝奖励了你们组%d遍" % (count)
else:
kill_desc = '周末就别提问了'
self.count.set(kill_desc) # 设置内容
self.window.update() # 屏幕更新
</pre>
当我们点击筛选后,再点击惩罚,就出现罚写的遍数了。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558768067799" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
image
六、日志实现
日志主要为了记录提问的人、什么时候提问的、罚写的遍数。我们都要把这些信息写入到本地,要不第二天老师忘记考谁了,学生也不承认提问我了,所以日志功能是必须的。 当最终提问人确定的时候调用savedesc函数 当点击惩罚按钮的时候调用savecount函数
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def savedesc(self, desc):
'''
负责把选中的人写入到log里面
:param desc:
:return:
'''
with open('log.txt', 'a', encoding='utf-8') as f:
f.write(self.gettime() + "\n" + desc)
def savecount(self, count):
'''
负责把被罚写的遍数写入到log里面
:param count:
:return:
'''
with open('log.txt', 'a', encoding='utf-8') as f:
f.write(str(count) + '\n')
f.write('--------------------------------\n')
</pre>
本地文件格式如下:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558768067807" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
image
有个记录挺好的,省着赖账。七、其他
基本功能就这些,我在界面上加了一些提示。比如今天的日期了,班级人数等等这些。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558768067814" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
image
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">全部代码:import tkinter as tk
import xlrd
import time
import random
import datetime
class LoveYou():
初始化
def init(self):
第1步,建立窗口window
self.window = tk.Tk()
第2步,给窗口的可视化起名字
self.window.title('班级考单词程序')
第3步,设定窗口的大小(长*宽)
self.window.geometry('600x400')
self.text = tk.StringVar() # 创建str类型
self.count = tk.StringVar()
self.data = self.read_data()
获取星期几
d = datetime.datetime.now()
self.day = d.weekday() + 1
def read_data(self):
'''
数据读取
:return:
'''
workbook = xlrd.open_workbook('1801.xlsx')
sheet1 = workbook.sheet_by_index(0) # sheet索引从0开始
data = list(sheet1.col_values(0)) # 读取第一列内容
['表格 1', '姓名', '小王', '老王', '小明', '小红', '小绿', '小青', '小陈', '小赵', '小王', '小李', '小孙', '小周', '小吴', '小钱', '小史', '小三', '小四', '小五', '小六']
data.pop(0) # 把表格 1去掉
data.pop(0) # 把姓名 去掉
return data
def take(self):
'''
负责随机抽取同学提问
:return:
'''
for s in range(50):
'''
后几秒慢点,制造紧张气氛
'''
desc = ''
if s == 47:
time.sleep(0.5)
elif s == 48:
time.sleep(0.6)
elif s == 48:
time.sleep(0.7)
elif s == 49:
time.sleep(0.9)
else:
time.sleep(0.1)
classes = random.sample(self.data, 2)
desc += "呦,你被上帝选中了:%s\n" % classes[0]
desc += "呦,你看着也很不错呀:%s\n" % classes[1]
if s == 49:
self.savedesc(desc) # 写入日志
self.text.set(desc) # 设置内容
self.window.update() # 屏幕更新
def kill(self):
'''
负责根据星期几选择不同惩罚遍数
:return:
'''
if self.day == 1:
count = random.randint(50, 100)
kill_desc = "上帝奖励了你们组%d遍" % (count)
elif self.day == 2:
count = random.randint(50, 120)
kill_desc = "上帝奖励了你们组%d遍" % (count)
self.count.set(kill_desc)
elif self.day == 3:
count = random.randint(50, 140)
kill_desc = "上帝奖励了你们组%d遍" % (count)
elif self.day == 4:
count = random.randint(50, 160)
kill_desc = "上帝奖励了你们组%d遍" % (count)
self.count.set(kill_desc)
elif self.day == 5:
count = random.randint(50, 180)
kill_desc = "上帝奖励了你们组%d遍" % (count)
else:
kill_desc = '周末就别提问了'
self.count.set(kill_desc) # 设置内容
self.window.update() # 屏幕更新
self.savecount(kill_desc) # 写入日志
def gettime(self):
'''
格式化时间
:return:
'''
return time.strftime('%Y-%m-%d', time.localtime(time.time())) + " 星期" + str(self.day)
def savedesc(self, desc):
'''
负责把选中的人写入到log里面
:param desc:
:return:
'''
with open('log.txt', 'a', encoding='utf-8') as f:
f.write(self.gettime() + "\n" + desc)
def savecount(self, count):
'''
负责把被罚写的遍数写入到log里面
:param count:
:return:
'''
with open('log.txt', 'a', encoding='utf-8') as f:
f.write(str(count) + '\n')
f.write('--------------------------------\n')
def main(self):
'''
主函数负责绘制
:return:
'''
绘制日期、班级总人数等
now = time.strftime('%Y-%m-%d', time.localtime(time.time())) + " 星期" + str(self.day)
now += "\n班级总人数:%s人" % str(len(self.data))
now += "\n正在合理计算中\n"
l1 = tk.Label(self.window, fg='red', text=now, width=500, height=5)
l1.config(font='Helvetica -%d bold' % 15)
l1.pack() # 安置标签
绘制筛选信息
l2 = tk.Label(self.window, fg='red', textvariable=self.text, width=500, height=3)
l2.config(font='Helvetica -%d bold' % 30)
l2.pack()
绘制惩罚信息
l3 = tk.Label(self.window, fg='red', textvariable=self.count, width=500, height=3)
l3.config(font='Helvetica -%d bold' % 20)
l3.pack()
绘制筛选按钮
btntake = tk.Button(self.window, text="筛选", width=15, height=2, command=self.take)
btntake.pack()
绘制惩罚按钮
btnkill = tk.Button(self.window, text="惩罚", width=15, height=2, command=self.kill)
btnkill.pack()
进入循环
self.window.mainloop()
if name == 'main':
loveyou = LoveYou()
loveyou.main()
</pre>
网友评论