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)中的名字

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()



  • 1
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二中卢老师

创作不易,你的鼓励是我最大的动

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值