Write a random roll call system in Python and package it into an exe file~

Lead:

When I was in college, many teachers would call names before or after class, and sometimes some students would be selected to answer questions during the class.

Today we use Python to implement a simple roll call system, and this article will share a random roll call system and package it into exe!

1. Realize random roll call

# -*- coding: UTF-8 -*-
"""

import tkinter as tk
from pandas import read_excel
from random import randint


# 读取数据
df1 = list(read_excel(r'.\学生名单_test.xls')['姓名'])
df2 = list(read_excel(r'.\学生名单_test.xls')['性别'])


def roll_call():    # 点名
    index_ = randint(0, len(df1) - 1)    # 产生随机索引
    name = df1.pop(index_)    # 弹出随机索引对应的姓名
    sex = df2.pop(index_)     # 弹出随机索引对应的性别
    t.insert('insert', f'{name}  {sex}\n')   # 插入到tkinter界面


win = tk.Tk()
# 设置窗口title和大小
win.title('随机点名系统')
win.geometry('600x600')

# Entry 单行文本
L = tk.Label(win, bg="yellow", text="随机点名系统", font=("KaiTi", 26), width=36, height=3)
L.place(x=0, y=0)

# 设置随机点名按钮 退出系统按钮
b1 = tk.Button(win, bg='red', text="随机点名", width=25, height=2, command=roll_call)
b1.place(x=80, y=200)
b2 = tk.Button(win, bg='red', text="退出系统", width=25, height=2, command=win.quit)
b2.place(x=325, y=200)

# Entry 单行文本
L = tk.Label(win, text="点到的学生名单如下", font=("KaiTi", 18), width=36, height=1)
L.place(x=90, y=315)

# 设置多行文本框  宽 高  文本框中字体  选中文字时文字的颜色
t = tk.Text(win, width=36, height=8, font=("KaiTi", 24), selectforeground='red')  # 显示多行文本
t.place(x=10, y=350)

win.mainloop()

The running effect is as follows:

Two, pyinstaller is packaged into exe

PyInstaller is a cross-platform Python application packaging tool that supports the three major platforms of Windows/Linux/MacOS and allows users to execute applications without installing Python.

pyinstaller install

pip install pyinstaller -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

pyinstaller packages python programs

PyInstaller is the easiest to use and only needs to specify the script file as the program entry.

After PyInstaller executes the packaging program, the following files and directories will be created in the current directory: main.spec file, whose prefix is ​​the same as the script name, specifies various parameters required for packaging; build subdirectory, which stores the files generated during the packaging process Temporary Files. The warnxxxx.txt file records the warning/error information during the build process. If there is a problem with PyInstaller running, you need to check the warnxxxx.txt file to get the details of the error. The xref-xxxx.html file outputs the module dependency graph obtained by the PyInstaller analysis script. The dist subdirectory stores the final generated files. If you use the single file mode, there will be only a single executable file; if you use the directory mode, there will be a subdirectory with the same name as the script, which is the real executable file and auxiliary files.

Enter the following code on the command line:

pyinstaller -F -i .icon图标文件路径 .py文件路径

-F | --onefile: generate a single executable file -i | --icon: specify an icon for the executable file

By default, it is generated on the C drive. Find the exe program with the icon in the dist folder, double-click to run it, and the program can be played after normal operation, indicating that the packaged program is successful.

The running effect is as follows:

   

3. Solve the RecursionError error when using pyinstaller to package the program

RecursionError: maximum recursion depth exceeded

Execute pyinstaller, although an error is reported, but the your_filename.spec file will be generated

pyinstaller -F your_filename.py

Find the your_filename.spec file on the C drive, open it for editing, and add the following statement

# 对递归深度进行设置
import sys
sys.setrecursionlimit(100000)

Execute pyinstaller and your_filename.spec file again

pyinstaller C:\Users\Administrator\your_filename.spec

Successfully packaged the python program into exe, which solved the problem.

epilogue

The above is the whole content of this article. If you have anything unclear, or if you need the complete project source code, you can private message me! Click on this row of blue fonts ! Thank you for your support to the editor.

Guess you like

Origin blog.csdn.net/a55656aq/article/details/122666327