為了上課時間能夠隨機抽點學生,就利用python做了這個隨機點名系統
實現的功能會讀取檔案裡內第乙個格式為.xlsx的檔案,如果該檔案裡面有姓名,學號兩列資料就能夠正常執行,注意列名必須為姓名,學號。如果點名的時間想放**,在資料夾內部放入***即可,需要將***名修改為"點名.***"
目前存在問題是啟動速度慢,不知道如何解決,存疑,後面解決了再改
import os
import tkinter as tk
import tkinter as tkinter
from tkinter import *
import tkinter.messagebox
import random
import pandas as pd
import pygame
from pandas import dataframe
path = "./"
files = os.listdir()
#print(len(files))
for i in range(0, len(files)):
try:
filetype = files[i].split(".")[1]
if filetype == "xls" or filetype == "xlsx":
filedata = files[i]
break
except:
print("有檔案沒有表明檔案型別")
else:
pass
test1 = pd.read_excel(filedata)
data3 = test1["姓名"]
data4 = test1["學號"]
data1 =
data2 =
data1[0:] = data4[1:]
data2[0:] = data3[1:]
#print(type(data))
going = true # 表示是否可以繼續滾動(遞迴)顯示下乙個名額
is_run = false # 表示當前**器是否在執行
# def lottery_roll(var1, var2):
def lottery_roll(var1):
global going
show_member1 = random.choice(data1) # 隨機選取姓名
show_member2 = random.choice(data2)
#dataframe.sample(1)
#print(type(show_member))
show_member = [int(show_member1),show_member2]
var1.set(show_member) # var1 顯示抽取出來的姓名
if going: # going=true
window.after(50, lottery_roll, var1) # 每50毫秒迴圈一次
else: # going=false
#result = tkinter.messagebox.showinfo(title='最終結果!', message='恭喜 {} !!!'.format(show_member)) # 彈窗顯示恭喜+抽取出來的姓名
going = true
return # 跳出def
def lottery_start(var1):
pygame.mixer.init()
try:
pygame.mixer.music.load(r"./點名.***") # 路徑
except:
pass
global is_run
if is_run: # is_run = true
return # 跳出def
is_run = true
lottery_roll(var1) # 執行迴圈抽取姓名的執行語句
pygame.mixer.music.set_volume(0.7) #控制**聲音的大小
def lottery_end():
global going, is_run
if is_run:
going = false
is_run = false
pygame.mixer.music.stop()
if __name__ == '__main__': # 如果模組是被直接執行的,則**塊被執行,如果模組是被匯入的,則**塊不被執行。
window = tkinter.tk()
window.title('點名v1.0')
window.geometry('450x320')
#window.geometry('450x320+250+15')
# bg_label = label(window, width=70, height=24, bg='#ecf5ff')
bg_label = label(window, width=70, height=24)
bg_label.place(anchor=nw, x=0, y=0)
var1 = strin**ar(value='誰會是天選之子')
#show_label1 = label(window, textvariable=var1, justify='left', anchor=center, width=17, height=3, bg='#bfefff',
# font='楷體 -40 bold', foreground='black')
show_label1 = label(window, textvariable=var1, justify='left', anchor=center, width=17, height=3,
font='華文行楷 -40 bold', foreground='black')
show_label1.place(anchor=nw, x=30, y=20)
button1 = button(window, text='開始', command=lambda: lottery_start(var1), width=14, height=2, bg='#a8a8a8',
font='華文仿宋 -18 bold')
button1.place(anchor=nw, x=32, y=175)
button2 = button(window, text='結束', command=lambda: lottery_end(), width=14, height=2, bg='#a8a8a8',
font='華文隸書 -18 bold')
button2.place(anchor=nw, x=232, y=175)
window.mainloop()
**寫完後,實驗pyinstaller -f -w命令將程式打包即可
執行效果如下
隨機點名系統
lang en charset utf 8 name viewport content width device width,initial scale 1.0 隨機點名系統title body th tab1 tab0 tab2 input style head tab0 width 1000 a...
教師點名系統
import xlrd import random import time isgo true count 1 while 1 data xlrd.open workbook c users administrator desktop 2.xlsx 引進excel檔案 table data.shee...
乙個網頁版的隨機點名系統
doctype html en utf 8 viewport content width device width,initial scale 1.0 text align center 隨機點名 title 設定網頁的背景顏色 body style head 設定網頁的標題 text align ...