python tkinter 随机抽奖程序

1. 界面

python tkinter 随机抽奖程序_第1张图片

import tkinter as tk
import random

a = ["荀彧","荀攸","贾诩","郭嘉","程昱","曹植","刘备","吕布"]
def start():
    L = len(a)
    if(L>0):
        x = random.randint(0,L-1)
        lbl1.place(x=140,y=100)
        lbl1['text'] = a[x]
        a.remove(a[x])
    else:
        lbl1.place(x=60,y=100)
        lbl1['text'] = '所有人均已中奖'

def reset():
    global a
    a = ["荀彧","荀攸","贾诩","郭嘉","程昱","曹植","刘备","吕布"]

root = tk.Tk()
root.title("随机抽奖")
root.geometry("400x300")

lbl1 = tk.Label(root,text="",font=("黑体",30))

btn1 = tk.Button(root,text=" 开 始 ",font=("宋体",24),command=start) 
btn1.place(x=120,y=150)

btn1 = tk.Button(root,text=" 重 置 ",font=("宋体",24),command=reset) 
btn1.place(x=120,y=210)
root.mainloop()

2. 读取文件(名单.txt)中的名字

python tkinter 随机抽奖程序_第2张图片

import tkinter as tk
import random

a = list(open("./名单.txt"))
def start():
    L = len(a)
    if(L>0):
        x = random.randint(0,L-1)
        lbl1.place(x=140,y=100)
        lbl1['text'] = a[x]
        a.remove(a[x])
    else:
        lbl1.place(x=60,y=100)
        lbl1['text'] = '所有人均已提问'

def reset():
    global a
    a = list(open("./名单.txt"))

root = tk.Tk()
root.title("随机点名")
root.geometry("400x300")

lbl1 = tk.Label(root,text="",font=("黑体",30))

btn1 = tk.Button(root,text=" 开 始 ",font=("宋体",24),command=start) 
btn1.place(x=120,y=150)

btn1 = tk.Button(root,text=" 重 置 ",font=("宋体",24),command=reset) 
btn1.place(x=120,y=210)
root.mainloop()

3. 滚动点名(可能重复)5秒内自动滚动,点击停止就停止滚动,5秒后不点击也会自动停止滚动

import tkinter as tk
import random,time

a = list(open("./名单.txt"))

def gundong():
    global t1,t2,running
    lbl1.place(x=90,y=50)
    lbl1['text'] = random.choice(a)
    t2 = time.time()
    if(t2-t1<5):
        if running:
            root.after(50,gundong)
    else:
        t1=0
        running=False
        btn1['text'] = ' 开 始 '

t1=0
running=False
def start():
    global t1,running
    if t1==0:
        t1 = time.time()
    if running:
        running = False
        btn1['text'] = ' 开 始 '
        t1=0
    else:
        running = True
        btn1['text'] = ' 停 止 ' 
    gundong()

root = tk.Tk()
root.title("随机抽奖")
root.geometry("400x300+200+100")

lbl1 = tk.Label(root,font=("黑体",30    ), justify='left', width=10, height=3, bg='#BFEFFF')
lbl1.place(x=90,y=50)

btn1 = tk.Button(root,text=" 开 始 ",font=("宋体",24),command=start) 
btn1.place(x=120,y=180)

root.mainloop()



你可能感兴趣的:(python,信息技术,python,开发语言)