代码如下:
import xlrd
import time
import random
import datetime
import tkinter as tk
class LoveYou():
def __init__(self):
self.window=tk.Tk()
self.window.title('计科18102班点名册')
self.window.geometry('600x400')
self.text = tk.StringVar()
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('C:/Users/Administrator/Desktop/python/jike18102.xlsx')
sheet1 = workbook.sheet_by_index(0)
data = list(sheet1.col_values(0))
data.pop(0)
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):
'''
:reeturn:
'''
return time.strftime('%Y-%m-%d', time.localtime(time.time())) + " 星期" + str(self.day)
def savedesc(self,desc):
'''
:param desc:
:return:
'''
with open('C:/Users/Administrator/Desktop/python/csb4.txt', 'a',encoding=('utf-8')) as f:
f.write(self.gettime() + "\n" + desc)
def savecount(self,count):
'''
:param count:
:return:
'''
with open('C:/Users/Administrator/Desktop/python/csb4.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()
错误提示如下: