整体思路:导入random模块
生成随机数
创建列表
用随机数索引列表
用easygui生成界面
显示对应的同学
代码:
import random
my_list = ['张三', '李四', '王五', '杨六']
x = random.randint(0, 3)
result = my_list[x]
# 用 Easygui 显示人员
import easygui as g
import sys
while 1:
g.msgbox('欢迎进入点名程序')
# note that we convert choice to string, in case
# the user cancelled the choice, and we got None.
g.msgbox('结果是:' + str(result))
msg = '你希望重新选择吗?'
title = '请选择'
if g.ccbox(msg, title): # show a Continue/Cancel dialog
pass # user chose Continue
else:
sys.exit(0) # user chose Cancel
效果:
网络上的代码:
#coding:utf-8
import pygame
import random
import time
import os,sys
from pygame.locals import *
def main():
L=['马江浩','李海力','熊晨','张勃扬','徐国庆','邓煜旗','张楷武','熊阳洋','谢宇','程辽','张绍喜','沙浩','李帅旗']
pygame.init()
t='点名啦'
ok='开始'
name=''
music=pygame.mixer.music.load('bg.mp3')
pygame.mixer.music.play(-1,100)
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption('随机点名')
myfont=pygame.font.Font('GB.ttc',60)
myfont1=pygame.font.Font('GB.ttc',80)
myfont2=pygame.font.Font('GB.ttc',50)
while True:
bg=pygame.image.load('22.jpg')
screen.blit(bg,(0,0))
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
keys=pygame.key.get_pressed()
if keys[K_RETURN]:
name=random.choice(L)
ok='结束'
else:
ok='开始'
textImg=myfont.render(t,True,(0,0,250))
textImg1=myfont.render(name,True,(0,0,250))
textImg2=myfont.render(ok,True,(0,0,250))
screen.blit(textImg,(300,120))
screen.blit(textImg1,(300,260))
screen.blit(textImg2,(320,400))
pygame.display.update()
main()
效果(未验证):
需安装pygame模块,终端terminal或cmd输入 pip3 install pygame即可