Fundamentals 10 min read

How to Build a Quick Python Lottery Draw Tool and Package It as an EXE

This article walks you through creating a simple Python lottery draw program, explains how to prepare the names CSV file, shows the GUI logic, provides key code snippets for random name generation and interface handling, and demonstrates packaging the script into a standalone executable with PyInstaller.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Build a Quick Python Lottery Draw Tool and Package It as an EXE

Introduction

At the end of the year a friend asked for a small "annual meeting lottery" tool, so I created a Python program that can draw winners from a list of employee names.

Demo

The program runs without requiring a Python environment on the target machine; you just double‑click the executable.

Function Logic

The program expects a names.csv file containing employee names. Place this file in the same directory as the program. If the file is missing, clicking the Yes button will generate a few random names for testing.

Usage Tips

Save employee names as a CSV file named names.csv .

You can edit the file with WPS or Office; the header row is ignored and the first column is read.

If duplicate names exist, the program will warn and exit – add an identifier such as a department or number to distinguish them.

Development Process

The project consists of two main scripts: one for generating random names and loading the name list, and another for the GUI.

1) Random Name Generation

File:

lottery_draw_func.py
def random_name():
    xing = '赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛' \
           '奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康' \
           '伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵' \
           '丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄曲家封芮羿储靳汲邴糜松井段富巫'
    ming = '伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清' \
           '飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善'
    result = []
    result.append(random.choice(xing))
    for i in range(random.randint(1, 2)):
        result.append(random.choice(ming))
    if random.randint(1, 30) == 1:
        result.append(random.choice(ming))
    return "".join(result)

2) GUI Development

File:

lottery_draw.py
if not os.path.exists("names.txt"):
    yes_no = sg.popup_yes_no(
        "待抽奖人员姓名名单文件names.txt没有找到!!!
请将所有的待抽奖人员姓名名单,"
        "
按行分割以gbk编码保存在names.txt文件中,
确认后重启程序!!!"
        "
或者你是否要让程序自动帮你生成随机姓名列表用于测试?",
        title="提示")
    if yes_no == "Yes":
        num = None
        while num is None:
            text = sg.popup_get_text("请输入要生成的姓名个数:")
            try:
                num = int(text)
            except:
                sg.popup("您输入的文本非数字或者点击了取消,请输入数字并点击确认", title="提示")
        write_random_name(num)
    else:
        sys.exit(0)
names = load_name_list()
counter = Counter(names)
name_count = counter.most_common(1)[0]
if name_count[1] != 1:
    sg.popup(f"本地文件data.txt存在同名同姓{ name_count[0] },
请手动改同名同姓加其他的值区分后,再重启程序继续", title="提示")
    sys.exit(0)
window = sg.Window('年会抽奖程序', layout, finalize=True)
window["in"].update(f"待抽奖名单({len(names)}人):")
winners = []
random.shuffle(names)
window["list1"].update(names)

Packaging the EXE

Prepare an icon file (e.g., a.ico) and place it in the same folder as the script.

Run the following command in the command line: pyinstaller -wF lottery_draw.py -i a.ico The resulting executable is about 9 MB and can be distributed directly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GUIPythonAutomationLotterypyinstaller
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.