这个代码是我从网上教程和自己的一些基础凑出来的一个抽奖程序,有兴趣的可以玩一下
图片:
代码:
from tkinter import *
import random
import os
import sys
import pyttsx3
import time
s = open('名单.txt','r',encoding='UTF-8')
name = s.read()
s.close()
name = name.split(' ')
state = 'open'
who = 0
engine = pyttsx3.init()
a = 1
b = -1
FileName = '名单.txt'
def randomly():
global name,state,who
if state == 'close':
playname()
return 0
rname = random.choice(name)
who = rname
lb.configure(text=rname)
root.after(100, randomly)
def order():
global b,name,state,who
if state == 'close':
playname()
return 0
b+=1
if b == len(name):
b = 0
who = name[b]
lb.configure(text=name[b])
root.after(100, order)
def sure():
global a
if a == 1:
randomly()
btn1.destroy()
btn2 = Button(root, text='确定', command=stop)
btn2.place(relx=0.37, rely=0.7, relwidth=0.3, relheight=0.1)
else:
order()
btn1.destroy()
btn2 = Button(root, text='确定', command=stop)
btn2.place(relx=0.37, rely=0.7, relwidth=0.3, relheight=0.1)
def stop():
global state
state = 'close'
def chose1():
global a
a=1
def chose2():
global a
a = 2
def again():
python = sys.executable
os.execl(python, python, *sys.argv)
def playname():
global who,engine
engine.say(who)
engine.runAndWait()
def change():
global t,FileName
start_directory = r'D:\kk\python\名单.txt'
os.system("explorer.exe %s" % start_directory)
t = os.stat(FileName).st_mtime
while 1:
# 判断时间戳是否变动
if str(t) != str(os.stat(FileName).st_mtime):
# 时间变动,保存新的时间戳
t = os.stat(FileName).st_mtime
cmd = ("TASKKILL /IM Notepad.exe")
os.system(cmd)
again()
def et():
sys.exit(0)
root = Tk()
root.title('抽签')
root.geometry('500x500+500+150')
lb = Label(root,text='',fg='blue',font=("黑体",80))
lb.pack()
var = IntVar()
rd1 = Radiobutton(root,text="随机选择",variable=var,value=0,command=chose1)
rd1.pack()
rd1.place(x=215,y=227)
rd2 = Radiobutton(root,text="顺序选择",variable=var,value=1,command=chose2)
rd2.pack()
rd2.place(x=215,y=250)
btn = Button(root, text='重置',command=again,width=0,height=0)
btn.pack()
btn.place(x=455,y=465)
btn1 = Button(root, text='就你了',command=sure)
btn1.place(relx=0.37, rely=0.7, relwidth=0.3, relheight=0.1)
btn3 = Button(root, text='退出',command=et,width=0,height=0)
btn3.pack()
btn3.place(x=5,y=465)
btn4 = Button(root, text='修改名单',command=change,width=0,height=0)
btn4.pack()
btn4.place(x=230,y=400)
root.mainloop()