#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import random
import time
import tkinter as tk
import xlrd
import xlwt
import pygame
import threading
from PIL import Image
from PIL import ImageTk
class Call_Name(object):
def __init__(self):
# 创建窗口
self.window = tk.Tk()
self.window.title("点名")
# 设置窗口大小
self.window.geometry('600x400')
self.window.iconbitmap('E:\python3_demo\点名神器\\11.ico')
# 创建str类型
self.text = tk.StringVar()
self.count = tk.StringVar()
self.create_data_book()
# self.time_string = self.now()
def create_data_book(self):
"""创建文件"""
try:
self.file_path = os.path.join(os.path.expanduser('~'), "Desktop")
self.path = ''.join([self.file_path,'\\','CalName'])
os.makedirs(self.path,exist_ok=True)
self.name = ''.join([self.path,'\\','callname.xls'])
if os.path.exists(self.name):
return
book = xlwt.Workbook()
sheet = book.add_sheet("花名册")
title = '姓名'
sheet.write(0, 0, title) # row,column
name_lists = ['熊大', '熊二', '光头强', '吉吉', '毛毛', '萝卜头', '蹦蹦', '涂涂', '肥波', '小狸', '翠花', '拖拖', '功夫大师', '老鳄', '嘟嘟',
'大马猴', '二狗', 'M集团老板', '团子', '抓白熊老板', '黑风', '象奶奶', '马戏团老板', '女盗贼(尚雯婕扮演)', '高盗贼(鲍春来扮演)',
'胖盗贼(孙红雷扮演)', 'BOSS大老板(曾舜晞扮演)', '小铁', '纳雅', '猴头菇', '大舌头(和光头强谈生意的商人)', '赵琳', '虎妞', '老赵头', '崽崽',
'藤球', '王老板', '光头强的女游客', '光头强他爸', '贝丝', '蛋壳侠', '凯特', '捷豆豆', '小月亮', '熊大熊二妈妈', '凯特爸', '凯特妈', '大黑熊老师',
'猫头鹰老师', '山羊爷爷', '天才威', '土拨鼠', '雪豹三兄弟', '稻田鸭', '特靠谱']
l = 1
for name in name_lists:
sheet.write(l, 0, name) # 写入前n 行
l += 1
book.save(self.name)
except Exception:
pass
def read_excel(self):
book = xlrd.open_workbook(self.name)
sheet = book.sheet_by_index(0)
rows = sheet.nrows
data_list = []
for i in range(rows):
# 获取前几行的内容
data_list.append(sheet.row_values(i)[0])
data_list.pop(0)
return data_list
def play_music(self):
try:
pygame.mixer.init() # 初始化
file = ''.join([self.path, '\\', 'bg.mp3'])
# file = r"E:\python3_demo\点名神器\bg.mp3"
pygame.mixer.music.load(file) # 加载文件
# print(track)
pygame.mixer.music.play()
except Exception:
pass
def choice_name(self):
# 创建线程播放音乐
t = threading.Thread(target=self.play_music)
t.start()
name_lists = self.read_excel()
for i in range(50):
con = ''
con1 = ''
if i == 47:
time.sleep(0.5)
elif i == 48:
time.sleep(0.8)
elif i == 48:
time.sleep(0.9)
elif i == 49:
time.sleep(1)
else:
time.sleep(0.1)
ret = random.sample(name_lists, 2)
self.window.update()
con += "哼!还有谁:%s" % ret[0]
con1 += " 呦,你看着也很不错呀:%s" % ret[1]
self.text.set(con)
self.count.set(con1)
self.window.update()
pygame.mixer.music.stop() # 停止播放
def now(self):
time_string = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
return time_string
def run(self):
time_string = self.now()
self.lab['text'] = time_string
self.window.after(1000, self.run)
def main(self):
# 绘制筛选信息
time_string = self.now()
l3 = tk.Label(self.window, fg='red', text=' 哼!还有谁:')
l3.config(font='Helvetica -30 bold')
l3.place(x=160, y=25)
l2 = tk.Label(self.window, fg='red', textvariable=self.text)
l2.config(font='Helvetica -30 bold')
l2.place(x=165, y=25)
# l2.place(260,25)
l4 = tk.Label(self.window, fg='red', text=' 呦,你看着也很不错呀:')
l4.config(font='Helvetica -30 bold')
l4.place(x=60, y=100)
# l3.place(185,100)
l5 = tk.Label(self.window, fg='red', textvariable=self.count)
l5.config(font='Helvetica -30 bold')
l5.place(x=65, y=100)
#
self.lab = tk.Label(self.window, text=time_string, fg='red')
self.lab.config(font='Helvetica -20 bold')
self.lab.place(x=400, y=340)
# 点名
icoimg = Image.open('E:\python3_demo\点名神器\\22.ico').resize((64, 64))
icoBtn = ImageTk.PhotoImage(image=icoimg)
btncall = tk.Button(self.window, command=self.choice_name, image=icoBtn)
btncall.config(cursor='hand2')
btncall.place(x=240, y=195)
self.window.after(1000, self.run)
self.window.mainloop() # 让窗体循环
if __name__ == '__main__':
Call_Name().main()
- 只要把callname.xls中花名册的姓名换成本班的花名册,就可以用了。同样也可以更改背景音乐。把要播放的的音乐改成bg.mp3 放到桌面的callname 文件下就行了。
转载:https://blog.csdn.net/weixin_44224529/article/details/104726970
查看评论