·创作背景
需要一个上课用来抽取幸运儿背书回答问题,课后供同学娱乐的程序。
·要求分析
·尽量不写bug
·使用图形化界面
·《公平公正》
·基本实现方法
·使用random模块进行抽取操作
·使用tkinter模块进行图形化界面绘制
·使用pil cv2模块进行图片视频处理
·使用os模块进行路径获取以及关闭窗口
·项目文件夹
创建文件夹一个,名称随意,在该文件夹下创建一个文件夹picture用来存放图片,再创建一个文件夹video存放视频文件
·实现代码
·导入模块
from tkinter import PhotoImage, Place
import tkinter as tk
from PIL import Image, ImageTk
import random
import cv2
from tkinter import Tk, Label, PhotoImage
import os
·编写函数video_play播放视频
def video_play (path):
video1 = cv2.VideoCapture(path)
while True:
ret, frame = video1.read()
if ret:
cv2.imshow('视频播放', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
video1.release()
cv2.destroyAllWindows()
·编写path函数获取路径
def path(path):
path = os.path.abspath(path)
return path
·编写 close_window函数关闭窗口(西沃白板较大点击x关闭较麻烦)
def close_window(event):
event.widget.destroy()
·编写抽取列表
a =[]
for i in range(0,30):
a.append(i)
for j in range(31,52):
a.append(j)
为了实现公平公正的目标,列表移除作者本人,如果需要加入1~51号,可以这样写
a=[]
for i in range(1,52)
a.append(i)
·编写单抽函数
def choice():
c = random.choice(a)
if c == 0 or c == 4 or c>51:
video_play(goldpath)
else:
print(purplepath)
video_play(purplepath)
re=tk.Toplevel()
re.geometry("1000x1000")
re.title('点击任意位置结束')
label = tk.Label(re, text=str(c))
label.config(font=("Arial", 60))
label.config(foreground="red")
label.place(x=480,y=480)
re.bind("<Button-1>", close_window)
a.remove(c)
·编写十连函数(极为草包的一段,满是bug,尽量不借鉴,用的少所以懒得改)
def choice10():
num=0
res=[]
while num<10:
c = random.choice(a)
#a.remove(c)
res.append(c)
a.remove(c)
num=num+1
print(res)
if 0 in res or 4 in res:
video_play(goldpath)
else:
video_play(purplepath)
re=tk.Toplevel()
re.geometry("1000x1000")
re.title('点击任意位置结束')
label = tk.Label(re, text=(res))
label.config(font=("Arial", 60))
label.config(foreground="red")
label.place(x=20,y=200)
re.bind("<Button-1>", close_window)
抽取动画函数中goldpath和purplepath变量分别指出金和出紫动画路径,其中出紫为普通学生,出金为《备受尊敬》《不会骂人》的老师,函数中参数可据实际情况修改优化。
·素材准备
在picture文件夹下准备主界面文件background.jpg文件,用作抽取器背景(可以放班级合照等)
像这样
将原来的抽取一次和抽取十次扣下分别命名为one.png和ten.png放在picture文件夹下
寻找素材出紫动画和出金动画分别命名为 purple.mp4 golden.mp4放入video文件夹中
golden.mp4需要提前在末尾加上老师的照片或动画(仅供娱乐!!!)
·编写窗口和调整按钮位置以及按键绑定
background=path('picture\\background.jpg')
ten=path('picture\\ten.png')
one=path('picture\\one.png')
root = tk.Tk()
root.geometry("1939x940")
image = Image.open(background)
photo = ImageTk.PhotoImage(image)
image0 = PhotoImage(file=one)
image1 = PhotoImage(file=ten)
label = tk.Label(root, image=photo)
label.pack()
y=13+17
if y in a:
a.remove(y)
button1 = tk.Button(root, image=image0, command=choice)
button1.place(x=1100, y=870)
button2 = tk.Button(root, image=image1, command=choice10)
button2.place(x=1525, y=870)
root.mainloop()
结束代码,根据需求调整参数和优化
·总体展示
#import tkinter
from tkinter import PhotoImage, Place
import tkinter as tk
#import tkinker as tk
from PIL import Image, ImageTk
import random
import cv2
import numpy as np
from tkinter import Tk, Label, PhotoImage
import os
def video_play (path):
video1 = cv2.VideoCapture(path)
while True:
ret, frame = video1.read()
if ret:
cv2.imshow('视频播放', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
video1.release()
cv2.destroyAllWindows()
a =[]
for i in range(0,31):
a.append(i)
for j in range(31,52):
a.append(j)
def path(path):
path = os.path.abspath(path)
return path
def close_window(event):
event.widget.destroy()
def choice():
c = random.choice(a)
if c == 0 or c == 4 or c>51:
video_play(goldpath)
else:
print(purplepath)
video_play(purplepath)
re=tk.Toplevel()
re.geometry("1000x1000")
re.title('点击任意位置结束')
label = tk.Label(re, text=str(c))
label.config(font=("Arial", 60))
label.config(foreground="red")
label.place(x=480,y=480)
re.bind("<Button-1>", close_window)
a.remove(c)
def choice10():
num=0
res=[]
while num<10:
c = random.choice(a)
#a.remove(c)
res.append(c)
a.remove(c)
num=num+1
print(res)
if 0 in res or 4 in res:
video_play(goldpath)
else:
video_play(purplepath)
re=tk.Toplevel()
re.geometry("1000x1000")
re.title('点击任意位置结束')
label = tk.Label(re, text=(res))
label.config(font=("Arial", 60))
label.config(foreground="red")
label.place(x=20,y=200)
re.bind("<Button-1>", close_window)
background=path('picture\\background.jpg')
ten=path('picture\\ten.png')
one=path('picture\\one.png')
root = tk.Tk()
root.geometry("1939x940")
image = Image.open(background)
photo = ImageTk.PhotoImage(image)
image0 = PhotoImage(file=one)
image1 = PhotoImage(file=ten)
label = tk.Label(root, image=photo)
label.pack()
y=13+17
if y in a:
a.remove(y)
button1 = tk.Button(root, image=image0, command=choice)
button1.place(x=1100, y=870)
button2 = tk.Button(root, image=image1, command=choice10)
button2.place(x=1525, y=870)
root.mainloop()
写的垃圾,仅供参考,部分使用ai帮助,如有雷同纯属巧合,如有侵权联系删除