[Python GUI Programming]——Student roll call program

In the process of computer programming, GUI (graphical user interface) is an essential part. There are multiple GUI libraries to choose from in Python, among which Tkinter is the standard GUI library that comes with Python, and it is powerful and easy to use. Therefore, in this blog, we will introduce a simple student roll call program developed using Tkinter.

  1. Main functions of the program

The main functions of the program include: reading the student list from the Excel file, randomly selecting a student for roll call, and counting and updating the number of roll calls and the latest roll call time of the student; at the same time, you can also add new students through the function. Add new students to the Excel file.

  1. Program implementation steps

First, the program will check whether the Excel file exists and whether the header is correct. If the file or header does not exist, a new file and header will be created. This step is very important to ensure the normal operation of the program.

Next, the window is created and divided into three parts. The top label says "Please click the 'Start Roll Call' button", the middle button is used to perform the roll call operation, and the bottom is a text box and an Add Student button for adding new students. When performing the roll call operation, the program will randomly select a student and display it on the interface. At the same time, the number of roll calls and the last roll call time of the student will also be updated in the Excel file. This step is achieved through the pandas module, which can quickly read and process Excel files.

In addition, the program also provides the function of adding new students. Users can add new students to the Excel file by entering their names and clicking the "Add Student" button. This step is also implemented through the pandas module, which can easily add new data rows.

  1. Program code analysis

The program code is very simple, the comments are detailed, and it is very friendly for beginners. Let's step by step analyze the main functions and code implementation of the program.

First, the program reads the Excel file and checks:

import pandas as pd
import random
import tkinter as tk
import datetime
import os

STUDENT_FILE = '学生名单.xlsx'

# 检查文件和表头是否存在,如果不存在则创建新的文件和表头
if not os.path.exists(STUDENT_FILE) or not {'姓名', '点名次数', '最近点名时间'}.issubset(set(pd.read_excel(STUDENT_FILE).columns)):
    df = pd.DataFrame(columns=['姓名', '点名次数', '最近点名时间'])
    df.to_excel(STUDENT_FILE, index=False)
else:
    # 读取Excel文件
    df = pd.read_excel(STUDENT_FILE)

Next, the program creates a Tkinter window and multiple labels, buttons and text boxes:

# 创建主窗口
root = tk.Tk()
root.title('学生点名程序')

# 创建标签1并放在主窗口中上方
label1 = tk.Label(root, text='请点击“开始点名”按钮', font=('Arial', 24))
label1.pack(expand=True)

# 创建标签2并放在主窗口中下方
label2 = tk.Label(root, text='', font=('Arial', 18))
label2.pack(pady=20)

# 点名函数
def roll_call():
    name = random.choice(df['姓名'])
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    df.loc[df['姓名'] == name, ['点名次数', '最近点名时间']] += 1, now
    update_label(name)

# 创建按钮1并放置在标签下方
button1 = tk.Button(root, text='开始点名', command=roll_call, font=('Arial', 18))
button1.pack(pady=20)

def add_student():
    name = entry.get().strip()
    if name:
        # 增加默认值
        df.loc[df.shape[0]] = [name, 0, '']
        df.to_excel(STUDENT_FILE, index=False)
        entry.delete(0, tk.END)

The program also provides a function to update the label, which is used to display the student who was called on and the number of roll calls and the latest roll call time of the student on the interface:

def update_label(name):
    count, time = df.loc[df['姓名'] == name, ['点名次数', '最近点名时间']].values[0]
    label1.config(text='被点名的学生是:{}'.format(name))
    label2.config(text='{} 已被点名 {} 次,最近一次是在 {}'.format(name, count, time))

Finally, there is the ability to add new students, which can be added to the Excel file by entering their name and clicking the "Add Student" button:

# 添加新学生函数
def add_student():
    name = entry.get().strip()
    if name:
        # 增加默认值
        df.loc[df.shape[0]] = [name, 0, '']
        df.to_excel(STUDENT_FILE, index=False)
        entry.delete(0, tk.END)

The above is the student roll call procedure introduced in this blog. Through this program, beginners can learn the basics of Python GUI programming and how to use the pandas module.

After reading, please click follow!

Your Xiao Xiao acridine! ! !

Guess you like

Origin blog.csdn.net/m0_55813592/article/details/130540913