利用 Tkinter 制作抽奖工具

之前在抖音上看到各种“奇葩”点名(提问),比如:学号尾数、学号和日期、抓阄等等,后来又看到艺赛旗发奖品也是使用机器人自动抽奖,然后自己突发奇想,使用 Tkinter 做一个自动抽奖工具,说干就干,效果如下:
利用 Tkinter 制作抽奖工具

利用 Tkinter 制作抽奖工具

废话不多说,上代码:

import tkinter as tk
import random

# with open("名单.txt", "rb") as f:
#     name_list = f.readlines()
#
# name_list = [name.decode().strip() for name in name_list if name.decode().strip()]
# print(name_list)

name_list = ['风清扬', '无崖子', '东邪', '西毒', '南帝', '北丐']
going = True
is_run = False


def lottery_roll(var1, var2):
    global going
    show_member = random.choice(name_list)
    var1.set(show_member)
    if going:
        window.after(50, lottery_roll, var1, var2)
    else:
        var2.set('恭喜 {} !!!'.format(show_member))
        going = True
        return


def lottery_start(var1, var2):
    global is_run
    if is_run:
        return
    is_run = True
    var2.set('幸运儿是你吗。。。')
    lottery_roll(var1, var2)


def lottery_end():
    global going, is_run
    if is_run:
        going = False
        is_run = False


if __name__ == '__main__':
    window = tk.Tk()
    # 设置大小和位置
    width, height = (405, 320)
    screenwidth = window.winfo_screenwidth()
    screenheight = window.winfo_screenheight()
    # 宽高及宽高的初始点坐标
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    window.geometry(size)
    window.title('   滚 动 抽 奖 器')
    bg_label = tk.Label(window, width=70, height=24, bg='#ECf5FF')
    bg_label.place(anchor="nw", x=0, y=0)
    var1 = tk.StringVar(value='即 将 开 始')
    show_label1 = tk.Label(window, textvariable=var1, justify='left', anchor="c", width=17, height=3, bg='#BFEFFF',
                        font='楷体 -40 bold', foreground='black')
    show_label1.place(anchor="nw", x=21, y=20)
    var2 = tk.StringVar(value='幸运儿是你吗。。。')
    show_label2 = tk.Label(window, textvariable=var2, justify='left', anchor="c", width=38, height=3, bg='#ECf5FF',
                        font='楷体 -18 bold', foreground='red')
    show_label2.place(anchor="nw", x=21, y=240)
    button1 = tk.Button(window, text='开始', command=lambda: lottery_start(var1, var2), width=14, height=2, bg='#A8A8A8',
                     font='宋体 -18 bold')
    button1.place(anchor="nw", x=20, y=175)
    button2 = tk.Button(window, text='结束', command=lambda: lottery_end(), width=14, height=2, bg='#A8A8A8',
                     font='宋体 -18 bold')
    button2.place(anchor="nw", x=232, y=175)
    window.mainloop()