首先附上源代碼
import tkinter as tk
import random
import threading
import time
from PIL import Image,ImageTk
import re
import os
#初始化視窗
def switch(): #亂數生成
root.flag=True
while root.flag:
i=random.randint(0, len(a)-1)
s=a[i]
s=re.split(' |\n',s)
image_file['file']=ps[i]
name['text']=s[0] #將s串列被切割后的第一個串列賦給name的text
code['text']=s[1]
time.sleep(0.5)
def butStartClick(): #開始
t=threading.Thread(target=switch) #多執行緒操作
t.start()
def btnStopClick(): #結束
root.flag=False
def main():
global root
global students,name,code,image_file,ps,a
root=tk.Tk()
root.title("隨機名單")
root.geometry('500x500+500+100')#+后面為視窗位置
root.resizable(False,False)
root.flag = True
canvas=tk.Canvas(root,width=500,height=500,bd=1,highlightthickness=0)
image_file=tk.PhotoImage(file='ps4.gif')
image=canvas.create_image(150,0,anchor='n',image=image_file) #anchor為錨點,放置在北
canvas.pack()
ps=['ps1.gif','ps2.gif','ps3.gif','ps4.gif','ps5.gif','ps6.gif'] #已準備好的gif圖片,并且在同一目錄下
name = tk.Label(root,text='某某某',font = ("華文行楷", 30,"normal"))
name.place(x=320,y=150,width=150,height=100)
code = tk.Label(root,text='xxx',font = ("宋體", 20,"normal"))
code['fg'] = 'red'
code.place(x=320,y=250,width=150,height=50)
os.chdir(r'C:\python專用') #讀取以下目錄
with open("花名冊.txt",'r',encoding='utf-8') as file: #打開檔案txt
a=file.readlines()
btnStart=tk.Button(root,text='開始',command=butStartClick)
btnStart.place(x=360, y=360, width=80, height=40)
butStop=tk.Button(root,text='停止',command=btnStopClick)
butStop.place(x=360, y=400, width=80, height=40)
root.mainloop()
if __name__ == '__main__' :
main()
txt檔案格式

圖片建議用ps轉為GIF格式,其他格式的話,圖片展示需要呼叫其他函式
最后實作如下
視頻不好放只能自己取運行啦

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/225964.html
標籤:python
下一篇:c++一些重要的細節
