Dare to skip class. The teacher made a random roll call system with Python. Do you still escape?

Posted by algarve4me on Sun, 30 Jan 2022 17:12:31 +0100

I will give you two opportunities to skip class. You must know what is more important than class.
For example, the Jianjia outside the building, or the moon tonight.
-- Mr. Zhou Heng

Introduction:

In college, many teachers will call the roll before or after class. Sometimes they will draw some students to answer questions in class.

Today, we use Python to implement a simple roll call system. Next, this article shares a random roll call system and packages it into exe!

1, Realize random roll call

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

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


#Read data
df1 = list(read_excel(r'.\Student list_test.xls')['full name'])
df2 = list(read_excel(r'.\Student list_test.xls')['Gender'])


def roll_call():    #Roll call
    index_ = randint(0, len(df1) - 1)    #Generate random index
    name = df1.pop(index_)    #Pop up the name corresponding to the random index
    sex = df2.pop(index_)     #Pop up the gender corresponding to the random index
    t.insert('insert', f'{name}  {sex}\n')   #Insert into tkinter interface


win = tk.Tk()
#Set the window title and size
win.title('Random roll call system')
win.geometry('600x600')

#Entry single line text
L = tk.Label(win, bg="yellow", text="Random roll call system", font=("KaiTi", 26), width=36, height=3)
L.place(x=0, y=0)

#Set random roll call button # exit system button
b1 = tk.Button(win, bg='red', text="Random roll call", width=25, height=2, command=roll_call)
b1.place(x=80, y=200)
b2 = tk.Button(win, bg='red', text="Exit the system", width=25, height=2, command=win.quit)
b2.place(x=325, y=200)

#Entry single line text
L = tk.Label(win, text="The list of students to be visited is as follows", font=("KaiTi", 18), width=36, height=1)
L.place(x=90, y=315)

#Set the width and height of the multiline text box, the font in the text box, and the color of the text when the text is selected
t = tk.Text(win, width=36, height=8, font=("KaiTi", 24), selectforeground='red')  #Show multiline text
t.place(x=10, y=350)

win.mainloop()

The operation effect is as follows:

2, Package pyinstaller into exe

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

pyinstaller installation

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

pyinstaller package python program

PyInstaller is the simplest to use. You only need 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 temporary files generated in the packaging process. warnxxxx.txt file records the warning / error information during the generation process. If there is a problem with the operation of PyInstaller, you need to check warnxxxx Txt file to get the details of the error. xref-xxxx.html file outputs the module dependency diagram obtained by PyInstaller analysis script. dist subdirectory to store the generated final files. If the single file mode is used, there will be only a single execution file; If the directory mode is used, there will be a subdirectory with the same name as the script, which is the real executable and affiliated files.

Enter the following code on the command line:

pyinstaller -F -i .icon Icon file path .py File path

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

By default, it is generated in disk C. find the exe program with icon in the dist folder, double-click it to run, and enter the program to play normally, indicating that the packaging program is successful.

The operation effect is as follows:

  

3, Solve the error of RecursionError when using pyinstaller packaging program

RecursionError: maximum recursion depth exceeded

Execute pyinstaller. Although an error is reported, your will be generated_ filename. Spec file

pyinstaller -F your_filename.py

Find your in drive C_ filename. Spec file, open it for editing, and add the following statement

#Set recursion depth
import sys
sys.setrecursionlimit(100000)

Execute pyinstaller and your again_ filename. Spec file

pyinstaller C:\Users\Administrator\your_filename.spec

The python program was successfully packaged into exe, which solved the problem.

epilogue

The above is the whole content of this article. If you don't know anything or need a complete project source code, you can confide in me! Click this line Blue font It's OK! Thank you for your support.

Topics: Python Back-end Programmer