以下是一个简单的 Python 日程表程序,可用于 Linux 系统:

  1. 首先,安装 Python 和相关依赖库(如 Tkinter)。

  2. 创建一个 Python 脚本文件,命名为'schedule.py'。

  3. 导入必要的库:

import tkinter as tk
from tkinter import messagebox
import datetime
import os
  1. 创建一个主窗口,并设置其标题和大小:
root = tk.Tk()
root.title('日程表')
root.geometry('400x400')
  1. 创建一个文本框,用于用户输入日期和事件:
date_label = tk.Label(root, text='日期 (YYYY-MM-DD):')
date_label.pack()

date_input = tk.Entry(root)
date_input.pack()

event_label = tk.Label(root, text='事件:')
event_label.pack()

event_input = tk.Entry(root)
event_input.pack()
  1. 创建一个按钮,用于添加事件到日程表:
def add_event():
    date = date_input.get()
    event = event_input.get()

    if len(date) != 10:
        messagebox.showerror('错误', '日期格式不正确!')
        return

    try:
        year, month, day = map(int, date.split('-'))
        datetime.datetime(year=year, month=month, day=day)
    except ValueError:
        messagebox.showerror('错误', '日期格式不正确!')
        return

    with open('schedule.txt', 'a') as f:
        f.write(f'{date} {event}
')

    messagebox.showinfo('成功', '事件已添加到日程表!')

add_button = tk.Button(root, text='添加事件', command=add_event)
add_button.pack()
  1. 创建一个按钮,用于显示当前日期的事件:
def show_events():
    today = datetime.date.today()

    with open('schedule.txt', 'r') as f:
        events = [line.strip() for line in f.readlines() if line.startswith(str(today))]

    if not events:
        messagebox.showinfo('提醒', '今天没有任何事件!')
        return

    messagebox.showinfo('今天的事件', '\n'.join(events))

show_button = tk.Button(root, text='今天的事件', command=show_events)
show_button.pack()
  1. 将日程表保存到文本文件'schedule.txt'中:
if not os.path.isfile('schedule.txt'):
    with open('schedule.txt', 'w') as f:
        pass
  1. 运行主循环:
root.mainloop()

完成以上步骤后,运行'schedule.py'文件即可使用日程表程序。用户可以输入日期和事件,并添加到日程表中。也可以显示当天的事件。日程表将保存在'schedule.txt'文件中。

用 Python 创建 Linux 日程表程序 - 简单指南

原文地址: https://www.cveoy.top/t/topic/nQwU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录