鉴于之前写的博客,很多人都问我源码,再次分享给大家,当时处于初学阶段,代码赘余和不规范蛮多的,多多包涵。
这个是从文件读取数据的,可以自行修改成数据库版本。
# -*- coding: utf-8 -*-
"""
@Time : 2019/12/6 11:52
@Author : Spider fu
@File : paiban.py
"""
import pandas as pd
import time, datetime
today = datetime.date.today()
today_time = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
tomorrow = today + datetime.timedelta(days=1)
after_tomorrow = today + datetime.timedelta(days=2)
"""
8-10
10-12
12-14
14-16
16-18
18-20
20-23
预约最多到第三天
取消预约需要提前一天
超时
"""
import tkinter.messagebox
from tkinter import ttk
from tkinter import *
from tkinter.messagebox import *
import os
import csv
path_data = os.getcwd()
path_order_list = path_data + '/data/order_list.csv'
path_user = path_data + '/data/user.csv'
def get_time():
day_list = [
str(today),
str(tomorrow),
str(after_tomorrow),
]
time_list = [
"8:00-10:00",
"10:00-12:00",
"12:00-14:00",
"14:00-16:00",
"16:00-18:00",
"18:00-20:00",
"20:00-23:00",
]
final_list = []
for one_day in day_list:
for one_time in time_list:
final_time = []
final_time.append(one_day)
final_time.append(one_time)
final_time.append("空闲")
final_list.append(final_time)
order_list = pd.read_csv(path_order_list, header=0, sep=',')
for order in order_list.values:
for final in final_list:
if final[0] == order[0] and final[1] == order[1]:
final[2] = "已被" + order[2] + "预约"
return final_list
def order(user_name, choose_day_flag, choose_time, nn):
flag_a = 1
panduan_time = re.match("\d*-\d*-\d* ?(\d*):\d*:\d*", str(today_time)).group(1)
print(panduan_time)
if choose_day_flag == '今天':
choose_day = today
elif choose_day_flag == '明天':
choose_day = tomorrow
else:
choose_day = after_tomorrow
order_time = str(choose_time)
order_day = str(choose_day)
order_list = pd.read_csv(path_order_list, header=0, sep=',')
order_error = 0
for order in order_list.values:
if order[1] == order_time and order[0] == order_day:
order_error = 1
if order_error == 0:
if choose_day_flag == "今天" and nn < int(panduan_time):
flag_a = 2
else:
f1 = [
order_day,
]
f2 = [
order_time,
]
f3 = [
user_name,
]
dataframe = pd.DataFrame({'order_day': f1, 'order_time': f2, 'user_name': f3})
# 将DataFrame存储为csv,index表示是否显示行名,default=True
dataframe.to_csv(path_order_list, mode='a', index=False, sep=',',header=False)
flag_a = 3
return flag_a
def quit_order(user_name, choose_day_flag, choose_time):
# choose_day_flag = input("请输入取消哪一天的预约,今天0,明天1,后天2:")
choose_day_flag = choose_day_flag
if choose_day_flag == '今天':
return 3
elif choose_day_flag == '明天':
choose_day = tomorrow
else:
choose_day = after_tomorrow
# choose_time_flag = input("请输入取消预约的时段编号:")
order_time = str(choose_time)
order_day = str(choose_day)
order_list = pd.read_csv(path_order_list, header=0, sep=',')
order_error = 0
for order in order_list.values:
if order[1] == order_time and order[0] == order_day:
order_error = 1
if order_error == 1:
# csv 写入
f1 = [
order_day,
]
f2 = [
order_time,
]
f3 = [
user_name,
]
dataframe = pd.DataFrame({'order_day': f1, 'order_time': f2, 'user_name': f3})
# 将DataFrame存储为csv,index表示是否显示行名,default=True
dataframe.to_csv(path_order_list, mode='a', index=False, sep=',', header=False)
return 1
else:
return 2
def is_name(name, password):
users = pd.read_csv(path_user, header=0, sep=',')
flag = 0
for user in users.values:
if user[0] == name and str(user[1]) == password:
flag = 1
break
if flag == 1:
return 1
else:
return 0
class MainWindow(Frame):
# 预约
def get_num1(self, event=None):
nn = int(re.match("(\d*):00-.*", self.comboxlist2.get()).group(1))
flag = order(self.user_name, self.comboxlist1.get(), self.comboxlist2.get(), nn)
if flag == 3:
tkinter.messagebox.showinfo("提示", "预约成功")
self.frame.destroy()
self.__init__(get_time(), self.user_name, self.password)
elif flag == 2:
tkinter.messagebox.showinfo("警告", "已过预约时间")
else:
tkinter.messagebox.showinfo("警告", "该时间已有人预约")
# 取消预约
def get_num2(self, event=None):
flag = quit_order(self.user_name, self.comboxlist1.get(), self.comboxlist2.get())
if flag == 1:
tkinter.messagebox.showinfo("提示", "取消成功")
self.frame.destroy()
self.__init__(get_time(), self.user_name, self.password)
elif flag == 2:
tkinter.messagebox.showinfo("提示", "该时间空闲")
else:
tkinter.messagebox.showinfo("提示", "请提前一天取消")
def __init__(self, final_time_list, name, password):
self.frame = Tk()
self.frame.title("预约系统")
self.user_name = name
self.day = StringVar()
self.time = StringVar()
self.password = password
self.comvalue1 = tkinter.StringVar() # 窗体自带的文本,新建一个值
self.comvalue2 = tkinter.StringVar() # 窗体自带的文本,新建一个值
# LoginPage.destroy(self)
# 定义标题
self.title1 = Label(self.frame, text="北邮实验预约系统")
self.title1.grid(row=0, column=0, padx=5, pady=5, columnspan=4)
# 获取当前时间
self.title2 = Label(self.frame, text="当前北京时间为:" + str(today_time))
self.title2.grid(row=5, column=3, padx=5, pady=5, columnspan=2)
# 选择时间
self.title2 = Label(self.frame, text="选择哪天")
self.title2.grid(row=1, column=0, padx=5, pady=5, columnspan=2)
self.title2 = Label(self.frame, text="预约的时段")
self.title2.grid(row=2, column=0, padx=5, pady=5, columnspan=2)
# 预约,取消预约,退出,三个按钮
self.button1 = Button(self.frame, text="预约", width=8, height=1, bg="green")
self.button2 = Button(self.frame, text="取消预约", width=8, height=1, bg="red")
self.button3 = Button(self.frame, text="退出", command=self.frame.destroy, width=8, height=1, bg="red")
self.button1.grid(row=3, column=0, padx=5, pady=1)
self.button2.grid(row=3, column=1, padx=5, pady=1)
self.button3.grid(row=3, column=2, padx=5, pady=1, columnspan=2)
self.button1.bind("<ButtonRelease-1>", self.get_num1) # 绑定函数get_num1
self.button2.bind("<ButtonRelease-1>", self.get_num2) # 绑定函数get_num2
# 选择哪一天,下拉列表,默认第一个
self.comboxlist1 = ttk.Combobox(self.frame, textvariable=self.comvalue1)
self.comboxlist1['values'] = ("今天", "明天", "后天")
self.comboxlist1.current(0)
self.comboxlist1.grid(row=1, column=2, padx=5, pady=5, columnspan=2)
# 选择时间段,下拉列表,默认第一个
self.comboxlist2 = ttk.Combobox(self.frame, textvariable=self.comvalue2)
self.comboxlist2['values'] = (
"8:00-10:00",
"10:00-12:00",
"12:00-14:00",
"14:00-16:00",
"16:00-18:00",
"18:00-20:00",
"20:00-23:00",
)
self.comboxlist2.grid(row=2, column=2, padx=5, pady=5, columnspan=2)
self.comboxlist2.current(0)
# 定义所有预定信息表格形式展示
self.tree = ttk.Treeview(self.frame) # #创建表格对象
self.tree["columns"] = (today, tomorrow, after_tomorrow) # #定义列
self.tree.column(today, width=100) # #设置列
self.tree.column(tomorrow, width=100)
self.tree.column(after_tomorrow, width=100)
self.tree.heading(today, text=today) # #设置显示的表头名
self.tree.heading(tomorrow, text=tomorrow)
self.tree.heading(after_tomorrow, text=after_tomorrow)
self.tree.insert("", 0, text="8:00-10:00", values=(final_time_list[0][2], final_time_list[7][2], final_time_list[14][2])) # #给第0行添加数据,索引值可重复
self.tree.insert("", 1, text="10:00-12:00", values=(final_time_list[1][2], final_time_list[8][2], final_time_list[15][2]))
self.tree.insert("", 2, text="12:00-14:00", values=(final_time_list[2][2], final_time_list[9][2], final_time_list[16][2]))
self.tree.insert("", 3, text="14:00-16:00", values=(final_time_list[3][2], final_time_list[10][2], final_time_list[17][2]))
self.tree.insert("", 4, text="16:00-18:00", values=(final_time_list[4][2], final_time_list[11][2], final_time_list[18][2]))
self.tree.insert("", 5, text="18:00-20:00", values=(final_time_list[5][2], final_time_list[12][2], final_time_list[19][2]))
self.tree.insert("", 6, text="20:00-23:00", values=(final_time_list[6][2], final_time_list[13][2], final_time_list[20][2]))
self.tree.grid(row=4, column=0, padx=5, pady=3, columnspan=4)
self.frame.mainloop()
class LoginPage(Frame):
def loginCheck(self, event=None):
name = self.bu1.get()
secret = self.bu2.get()
if name == "":
showinfo(title='错误', message='账号不能为空!')
elif secret == "":
showinfo(title='错误', message='密码不能为空!')
elif is_name(name, secret):
self.frame.destroy()
MainWindow(get_time(), name, secret)
else:
showinfo(title='错误', message='账号或密码错误!')
def __init__(self):
self.frame = Tk()
self.frame.title('登录')
self.width = 280
self.height = 200
self.screenwidth = self.frame.winfo_screenwidth()
self.screenheight = self.frame.winfo_screenheight()
self.alignstr = '%dx%d+%d+%d' % (self.width, self.height, (self.screenwidth - self.width) / 3, (self.screenheight - self.height) / 3)
self.frame.geometry(self.alignstr) # 居中对齐
self.username = StringVar()
self.password = StringVar()
self.lable1 = Label(self.frame).grid(row=0, stick=W, pady=10)
self.lable2 = Label(self.frame, text='账户: ').grid(row=1, stick=W, pady=10)
self.bu1 = Entry(self.frame, textvariable=self.username)
self.bu1.grid(row=1, column=1, stick=E)
self.lable3 = Label(self.frame, text='密码: ').grid(row=2, stick=W, pady=10)
self.bu2 = Entry(self.frame, textvariable=self.password, show='*')
self.bu2.grid(row=2, column=1, stick=E)
self.bu3 = Button(self.frame, text='登陆', command=self.frame.destroy)
self.bu3.grid(row=3, stick=W, pady=10)
self.bu3.bind("<ButtonRelease-1>", self.loginCheck)
self.bu4 = Button(self.frame, text='退出', command=self.frame.quit).grid(row=3, column=1, stick=E)
self.frame.mainloop()
if __name__ == "__main__":
LoginPage()