account.py
import time
import socket
import json
class Account:
def __init__(self, name, pw):
self.name = name
self.pw = pw # pw == -1 为默认密码
self.time = str("2022.6.6")
self.appointments = []
self.mode = 0
js=json.dumps(
{
'type':"login",
'msg1':self.name,
'msg2':self.pw
}
)
from passinfromation import pass_main
jk = pass_main(js)
self.jd = json.loads(jk)
def get_appointment(self):
"""从数据库读出预约信息"""
js = json.dumps(
{
'type': 'get appointment',
'msg1': self.name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if not jd:
self.appointments = (("故宫", "第二个", "颐和园"),
("2022.03.01", "2022.04.04", "2022.08.01"))
else:
self.appointments = jd['msg1']
return self.appointments
def cancel_appointment(self, attr_name, appoint_time):
# 从db中删除预约
js = json.dumps(
{
'type': 'create account',
'msg1': self.name,
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if jd['msg2'] == 'False':
return False
return True
def is_in_db(self):
"""判断输入的账户是否在数据库中"""
if (self.jd['msg1']=="True" and self.jd['msg2']=="登陆成功") or (
self.jd['msg1']=='False' and self.jd['msg2'=='密码错误']):
return True
else:
return False
def is_manager(self):
"""判断该账户是否为管理员"""
js = json.dumps(
{
'type': 'is manager',
'msg1': self.name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if jd['msg1'] == 'False':
return False
return True
def create_account(self):
"""将账户加入数据库"""
js = json.dumps(
{
'type': 'create account',
'msg1': self.name,
'msg2': self.pw
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if jd['msg1'] == 'False':
return False
return True
def get_name(self):
"""获得账户信息"""
return self.name
def get_time(self):
"""获得注册时间"""
js = json.dumps(
{
'type': 'get register time',
'msg1': self.name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if jd:
self.time = jd['msg1']
return self.time
def cancellation(self):
"""账户从数据库中注销"""
js = json.dumps(
{
'type': 'cancellation the account',
'msg1': str(self.name)
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if jd['msg1'] == 'False':
return False
else:
return True
def is_pw_correct(self):
"""判断密码是否正确"""
if self.jd['msg1']=='False' and self.jd['msg2']=='密码错误':
return False
elif self.jd['msg1']=="True" and self.jd['msg2']=="登陆成功":
return True
attraction.py
import tkinter as tk
import window_inter
import button
import account
import tkinter.messagebox
import json
class Attraction:
def __init__(self, user_name, pw, mode, parent_window):
self.window = tk.Toplevel(parent_window)
# self.window.withdraw()
self.user_name = user_name
self.pw = pw
self.account_now = account.Account(self.user_name, self.pw)
self.mode = mode # 0:user mode 1:manager mode
self.details = ("景点名称", "景点描述", "票价", "开放情况", "景点热度")
self.attr_name = ' '
self.confirm_butt = tk.Button(self.window, text='确定')
self.selection = button.EntryButton(self.window, '景点名称:', None, 60)
# 用户模式下的变量
# 推荐的十个景点
self.recommend_butt = []
self.recommend = []
# 管理员模式下的变量
# self.find_butt = button.EntryButton(self.window, '景点名:', None, 60)
self.find_attraction_for_manager = tk.Button(self.window, text="查询景点", font=("宋体", 15))
self.find_attraction_for_manager.bind("<1>", lambda event: self.press_find_attraction_for_manager())
self.adapt_attraction = tk.Button(self.window, text="景点修改", font=("宋体", 15))
self.adapt_attraction.bind("<1>", lambda event: self.press_adapt_attraction())
self.get_appointment = tk.Button(self.window, text="预约记录", font=("宋体", 15))
self.get_appointment.bind("<1>", lambda event: self.press_get_appointment())
self.delete_butt = tk.Button(self.window, text="删除", font=("黑体", 15))
self.insert_butt = tk.Button(self.window, text="添加", font=("黑体", 15))
self.insert_butt.bind("<1>", lambda event: self.press_insert())
# self.confirm_view()
def get_attraction_window(self):
return self.window
def confirm_view(self):
# self.rank_butt.bind('<1>', self.press_rank)
if not self.mode:
self.view_for_user()
else:
self.view_for_manager()
# self.window.mainloop()
def is_in_db(self, attr_name):
# 判断景点是否在db中
js = json.dumps(
{
'type': 'is attraction in db',
'msg1': attr_name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if jd['msg1'] =='True':
return True
else:
return False
def view_for_user(self):
window_inter.WindowInter(self.window, '查看景点', 500, 400).show()
self.selection.show(40, 100)
self.confirm_butt.bind('<1>', lambda event: self.find_attraction(self.attr_name))
self.confirm_butt.place(x=320, y=95)
self.set_recommend()
def view_for_manager(self):
# 清除别的按钮
self.selection.forget()
self.confirm_butt.place_forget()
self.delete_butt.place_forget()
self.insert_butt.place_forget()
window_inter.WindowInter(self.window, '管理景点', 500, 400).show()
self.find_attraction_for_manager.grid(row=4, column=1)
self.adapt_attraction.grid(row=4, column=3)
self.get_appointment.grid(row=4, column=5)
"""管理员 景点改查"""
def press_find_attraction_for_manager(self):
# 清除别的按钮
self.selection.forget()
self.confirm_butt.place_forget()
self.delete_butt.place_forget()
self.insert_butt.place_forget()
self.selection.show(40, 40)
self.confirm_butt.bind("<1>", lambda event: self.find_attraction(''))
self.confirm_butt.place(x=300, y=40)
"""用户 推荐景点"""
def set_recommend(self):
# 应从db中取值
js = json.dumps(
{
'type': 'get recommend',
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
recommend = jd['msg1']
if not recommend:
self.recommend = ('故宫', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten')
else:
self.recommend = recommend
x = 40
y = 150
for i in range(10):
attract_label = tk.Label(self.window, text=self.recommend[i], font=('宋体', 10, "underline"))
attract_label.place(x=x, y=y)
self.recommend_butt.append(attract_label)
# self.recommend_butt[i].bind('<1>', lambda event: self.press_commend())
x += 60
if i == 4:
x = 40
y += 40
self.recommend_butt[0].bind('<1>', lambda event: self.find_attraction(self.recommend[0]))
self.recommend_butt[1].bind('<1>', lambda event: self.find_attraction(self.recommend[1]))
self.recommend_butt[2].bind('<1>', lambda event: self.find_attraction(self.recommend[2]))
self.recommend_butt[3].bind('<1>', lambda event: self.find_attraction(self.recommend[3]))
self.recommend_butt[4].bind('<1>', lambda event: self.find_attraction(self.recommend[4]))
self.recommend_butt[5].bind('<1>', lambda event: self.find_attraction(self.recommend[5]))
self.recommend_butt[6].bind('<1>', lambda event: self.find_attraction(self.recommend[6]))
self.recommend_butt[7].bind('<1>', lambda event: self.find_attraction(self.recommend[7]))
self.recommend_butt[8].bind('<1>', lambda event: self.find_attraction(self.recommend[8]))
self.recommend_butt[9].bind('<1>', lambda event: self.find_attraction(self.recommend[9]))
"""管理员 用户 景点信息查看"""
def find_attraction(self, attr_name):
# 在db中找到景点
js = json.dumps(
{
'type': 'find attraction',
'msg1': attr_name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if not jd:
scription = 'good'
price = 1.2
is_open = True
heat = 'very'
else:
scription = jd['scription']
price = jd['price']
is_open = jd['is_open']
heat = jd['heat']
self.attr_name = self.selection.get_contain_immediate()
if self.attr_name:
attr_name = self.attr_name
# 输出信息
attr_window = tk.Toplevel(self.window)
adapt_0 = tk.Label(attr_window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt_1 = tk.Label(attr_window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt_2 = tk.Label(attr_window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt_3 = tk.Label(attr_window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt_4 = tk.Label(attr_window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt = (adapt_0, adapt_1, adapt_2, adapt_3, adapt_4)
contains = []
window_inter.WindowInter(attr_window, '景点信息', 500, 400).show()
# 显示景点名称
tk.Label(attr_window, text='景点名称:', font=('宋体', 16)).grid(column=0, row=0)
contain_0 = tk.Label(attr_window, text=str(attr_name), font=('宋体', 16))
contain_0.grid(column=1, row=0)
contains.append(contain_0)
if self.mode:
adapt[0].grid(row=0, column=6)
adapt[0].bind("<1>", lambda event: self.press_adapt("景点名称", attr_window, adapts=adapt, contains=contains))
# 显示景点描述
tk.Label(attr_window, text='景点描述:', font=('宋体', 16)).grid(column=0, row=2)
contain_1 = tk.Label(attr_window, text=scription, font=('宋体', 16))
contain_1.grid(column=1, row=2)
contains.append(contain_1)
if self.mode:
adapt[1].grid(row=2, column=6)
adapt[1].bind("<1>", lambda event: self.press_adapt("景点描述", attr_window, adapts=adapt, contains=contains))
# 显示景点票价
tk.Label(attr_window, text='票价:', font=('宋体', 16)).grid(column=0, row=4)
contain_2 = tk.Label(attr_window, text=str(price), font=('宋体', 16))
contain_2.grid(column=1, row=4)
contains.append(contain_2)
if self.mode:
adapt[2].grid(row=4, column=6)
adapt[2].bind("<1>", lambda event: self.press_adapt("票价", attr_window, adapts=adapt, contains=contains))
# 显示景点开放情况
tk.Label(attr_window, text='开放情况:', font=('宋体', 16)).grid(column=0, row=6)
if is_open:
contain_3 = tk.Label(attr_window, text='The attraction is open now', font=('宋体', 16))
contain_3.grid(column=1, row=6)
contains.append(contain_3)
else:
contain_3 = tk.Label(attr_window, text='The attraction is close now', font=('宋体', 16))
contain_3.grid(column=1, row=6)
contains.append(contain_3)
if self.mode:
adapt[3].grid(row=6, column=6)
adapt[3].bind("<1>", lambda event: self.press_adapt("开放情况", attr_window, adapts=adapt, contains=contains))
# 显示景点热度
tk.Label(attr_window, text='景点热度:', font=('宋体', 16)).grid(column=0, row=8)
contain_4 = tk.Label(attr_window, text=heat, font=('宋体', 16))
contain_4.grid(column=1, row=8)
contains.append(contain_4)
if self.mode:
adapt[4].grid(row=8, column=6)
adapt[4].bind("<1>", lambda event: self.press_adapt("景点热度", attr_window, adapts=adapt, contains=contains))
def press_adapt(self, name_to_change, window, adapts, contains):
contain = tk.StringVar()
adapt = tk.Entry(window, textvariable=contain, font=('宋体', 15))
confirm_butt = tk.Button(window, text='确定', font=("黑体", 15))
if name_to_change == "景点名称":
# 修改db
adapts[0].grid_forget()
contains[0].grid_forget()
adapt.grid(column=1, row=0)
confirm_butt.grid(row=0, column=7)
confirm_butt.bind("<1>", lambda event: self.confirm_to_adapt(
name_to_change=name_to_change, attr_name=contain,
window=window, entry=adapt, confirm=confirm_butt, contains=contains))
elif name_to_change == "景点描述":
# 修改db
adapts[1].grid_forget()
contains[1].grid_forget()
adapt.grid(column=1, row=2)
confirm_butt.grid(row=2, column=7)
confirm_butt.bind("<1>", lambda event: self.confirm_to_adapt(
name_to_change=name_to_change, attr_name=contain,
window=window, entry=adapt, confirm=confirm_butt, contains=contains))
elif name_to_change == "票价":
# 修改db
adapts[2].grid_forget()
contains[2].grid_forget()
adapt.grid(column=1, row=4)
confirm_butt.grid(row=4, column=7)
confirm_butt.bind("<1>", lambda event: self.confirm_to_adapt(
name_to_change=name_to_change, attr_name=contain,
window=window, entry=adapt, confirm=confirm_butt, contains=contains))
elif name_to_change == "开放情况":
# 修改db
adapts[3].grid_forget()
contains[3].grid_forget()
adapt.grid(column=1, row=6)
confirm_butt.grid(row=6, column=7)
confirm_butt.bind("<1>", lambda event: self.confirm_to_adapt(
name_to_change=name_to_change, attr_name=contain,
window=window, entry=adapt, confirm=confirm_butt, contains=contains))
elif name_to_change == "景点热度":
# 修改db
adapts[4].grid_forget()
contains[4].grid_forget()
adapt.grid(column=1, row=8)
confirm_butt.grid(row=8, column=7)
confirm_butt.bind("<1>", lambda event: self.confirm_to_adapt(
name_to_change=name_to_change, attr_name=contain,
window=window, entry=adapt, confirm=confirm_butt, contains=contains))
def confirm_to_adapt(self, name_to_change, attr_name, window, entry, confirm, contains):
entry.grid_forget()
confirm.grid_forget()
adapt_0 = tk.Label(window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt_1 = tk.Label(window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt_2 = tk.Label(window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt_3 = tk.Label(window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt_4 = tk.Label(window, text='修改', font=('宋体', 16, "underline"), fg="red")
adapt = (adapt_0, adapt_1, adapt_2, adapt_3, adapt_4)
if name_to_change == "景点名称":
js = json.dumps(
{
'type': 'adapt name',
'msg1': self.attr_name,
'msg2': attr_name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
self.attr_name = attr_name
contains[0] = tk.Label(window, textvariable=attr_name, font=('宋体', 16))
contains[0].grid(column=1, row=0)
adapt[0].grid(row=0, column=6)
adapt[0].bind("<1>", lambda event: self.press_adapt("景点名称", window, adapts=adapt, contains=contains))
elif name_to_change == "景点描述":
js = json.dumps(
{
'type': 'adapt scription',
'msg1': self.attr_name,
'msg2': attr_name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
contains[1] = tk.Label(window, textvariable=attr_name, font=('宋体', 16))
contains[1].grid(column=1, row=2)
adapt[1].grid(row=2, column=6)
adapt[1].bind("<1>", lambda event: self.press_adapt("景点描述", window, adapts=adapt, contains=contains))
elif name_to_change == "票价":
js = json.dumps(
{
'type': 'adapt price',
'msg1': self.attr_name,
'msg2': attr_name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
contains[2] = tk.Label(window, textvariable=attr_name, font=('宋体', 16))
contains[2].grid(column=1, row=4)
adapt[2].grid(row=4, column=6)
adapt[2].bind("<1>", lambda event: self.press_adapt("票价", window, adapts=adapt, contains=contains))
elif name_to_change == "开放情况":
js = json.dumps(
{
'type': 'adapt open condition',
'msg1': self.attr_name,
'msg2': attr_name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
contains[3] = tk.Label(window, textvariable=attr_name, font=('宋体', 16))
contains[3].grid(column=1, row=6)
adapt[3].grid(row=6, column=6)
adapt[3].bind("<1>", lambda event: self.press_adapt("开放情况", window, adapts=adapt, contains=contains))
elif name_to_change == "景点热度":
js = json.dumps(
{
'type': 'adapt heat',
'msg1': self.attr_name,
'msg2': attr_name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
contains[4] = tk.Label(window, textvariable=attr_name, font=('宋体', 16))
contains[4].grid(column=1, row=8)
adapt[4].grid(row=8, column=6)
adapt[4].bind("<1>", lambda event: self.press_adapt("景点热度", window, adapts=adapt, contains=contains))
"""管理员 景点增删"""
def press_adapt_attraction(self):
# 清除别的按钮
self.selection.forget()
self.confirm_butt.place_forget()
self.delete_butt.place_forget()
self.insert_butt.place_forget()
self.selection.show(40, 50)
self.confirm_butt.bind("<1>", lambda event: self.adapt())
self.confirm_butt.place(x=300, y=40)
def adapt(self):
# 获得要修改的景点名称
self.attr_name = self.selection.get_contain_immediate()
if self.is_in_db(self.attr_name):
self.delete_butt.bind("<1>", lambda event: self.press_delete())
self.delete_butt.place(x=200, y=100)
else:
self.insert_butt.place(x=350, y=40)
self.insert_butt.bind("<1>", lambda event: self.press_insert())
return "break"
"""管理员 景点删除"""
def press_delete(self):
msg = tkinter.messagebox.askokcancel("警告", "确定删除该景点?")
if msg:
# 从db中删除景点
name = self.selection.get_contain_immediate()
js = json.dumps(
{
'type': 'delete attraction',
'msg1': name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
tkinter.messagebox.showinfo('提示', '删除成功')
return "break"
"""管理员 景点增加"""
def press_insert(self):
msg = tkinter.messagebox.askokcancel("警告", "确定添加该景点?")
y = 60
insert_entry = [] # 获取修改内容
if msg:
self.insert_butt.place_forget()
for i in self.details:
butt = button.EntryButton(self.window, i+':', None, 60)
butt.show(x=40, y=y)
insert_entry.append(butt)
y += 20
self.confirm_butt.place(x=250, y=200)
self.confirm_butt.bind("<1>", lambda event: self.confirm_to_insert(insert_entry))
return "break"
def confirm_to_insert(self, insert_entry):
insert_detail = []
tkinter.messagebox.showinfo(title='提示', message='创建成功')
for i in insert_entry:
insert_detail.append(i.get_contain_immediate())
i.forget()
# 从db中添加该景点
js = json.dumps(
{
'type': 'insert attraction',
'msg1': self.name,
'msg2': insert_detail
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
self.view_for_manager()
return "break"
"""管理员 景点预约记录"""
def press_get_appointment(self):
# 清除别的按钮
self.selection.forget()
self.confirm_butt.place_forget()
self.delete_butt.place_forget()
self.insert_butt.place_forget()
self.selection.show(40, 40)
self.confirm_butt.place(x=300, y=40)
attr_name = self.selection.get_contain_immediate()
self.confirm_butt.bind("<1>", lambda event: self.confirm_to_appoint(attr_name, self.is_in_db(attr_name)))
def confirm_to_appoint(self, attr_name, is_in_db):
warning = tk.Label(self.window, text='没有该景点!', font=("黑体", 15, "underline"), fg="red")
if not is_in_db:
warning.place(x=200, y=100)
else:
warning.place_forget()
appoint_window = tk.Toplevel(self.window)
window_inter.WindowInter(appoint_window, '景点'+str(attr_name)+'预约记录', 500, 400).show()
# 从db中调出预约信息
js = json.dumps(
{
'type': 'get appointment of attraction',
'msg1': attr_name
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if not jd['msg1']:
appoint_name = ("张三", "李四", "王麻子")
appoint_time = ("2022.1.1", "2022.2.2", "2022.3.3")
# appoint_attraction = ("颐和园", "故宫", "长城")
else:
appoint_name = jd['msg1']
appoint_time = jd['msg2']
adapt_appoint = []
for i in range(len(appoint_name)):
tk.Label(appoint_window, text=appoint_name[i], font=("宋体", 15)).grid(row=i * 2, column=1)
tk.Label(appoint_window, text=appoint_time[i], font=("宋体", 15)).grid(row=i * 2, column=3)
# tk.Label(appoint_window, text=appoint_attraction[i], font=("宋体", 15)).grid(row=i * 2, column=3)
adapt_appoint.append(tk.Label(appoint_window, text='取消预约', font=("黑体", 15, "underline"), fg="red"))
adapt_appoint[i].grid(row=i * 2, column=5)
adapt_appoint[i].bind(
"<1>", lambda event: self.confirm_to_cancel_appoint(
appoint_name, appoint_time, appoint_window, attr_name))
return "break"
def confirm_to_cancel_appoint(self, appoint_name, appoint_time, window, attr_name):
msg = tkinter.messagebox.askokcancel("警告", "确定删除该预约?")
if msg:
# 从db中删除景点预约
account.Account(self.user_name, self.pw).cancel_appointment(attr_name=attr_name, appoint_time=appoint_time)
tkinter.messagebox.showinfo('提示', '删除成功')
self.confirm_to_appoint(attr_name, 1)
window.destroy()
return "break"
"""管理员 数据统计"""
"""管理员 区域景点记录"""
def get_area_attraction(self):
# 从db中获得景点记录
js = json.dumps(
{
'type': 'get area attraction'
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if not jd['msg1']:
contains = (("朝阳区", "1234"),
("天河区", "4321"),
("xx区", "1111"))
else:
contains = jd['msg1']
return contains
"""管理员 开放情况"""
def get_open_condition(self):
# 从db中获得开放记录
js = json.dumps(
{
'type': 'get open condition of all'
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if not jd['msg1']:
contains = ("111", "222") # 开放,未开放
else:
contains = jd['msg1']
return contains
"""管理员 所有景点票价"""
def get_price_of_all(self):
# 从db中获得票价记录
js = json.dumps(
{
'type': 'get price of all'
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if not jd['msg1']:
contains = (("0 - 10", "10"),
("10 - 50", "11"),
("50 - 100", "12"),
("100以上", "13"))
else:
contains = jd['msg1']
return contains
bus_route.py
import tkinter as tk
from typing import Tuple
import window_inter
import button
import attractions
import json
class BusRoute:
def __init__(self, parent_window):
self.window = tk.Toplevel(parent_window)
window_inter.WindowInter(self.window, '查询巴士路线', 500, 400).show()
self.start = button.EntryButton(self.window, '起始景点:', None, 90)
self.start.show(40, 100)
self.start_name = self.start.get_contain_immediate()
self.terminal = button.EntryButton(self.window, '终止景点', None, 90)
self.terminal.show(40, 200)
self.terminal_name = self.terminal.get_contain_immediate()
self.confirm_butt = tk.Button(self.window, text='确认', font=('宋体', 15))
self.confirm_butt.place(x=150, y=300)
"""
self.return_butt = tk.Button(self.window, text='返回', font=('宋体', 15))
self.return_butt.place(x=200, y=300)
"""
self.warning = tk.Label(self.window, text='')
def press_confirm(self, event):
self.start_name = self.start.get_contain_immediate()
self.terminal_name = self.terminal.get_contain_immediate()
start_attr = attractions.Attraction(0, 0, 0, self.window)
terminal_attr = attractions.Attraction(0, 0, 0, self.window)
start_attr.get_attraction_window().withdraw()
terminal_attr.get_attraction_window().withdraw()
if not self.start_name:
self.warning.config(text='景点名不能为空')
self.warning.place(x=350, y=100)
elif not self.terminal_name:
self.warning.config(text='景点名不能为空')
self.warning.place(x=350, y=200)
elif not start_attr.is_in_db(self.start_name):
self.warning.config(text='没有该景点')
self.warning.place(x=350, y=100)
elif not terminal_attr.is_in_db(self.terminal_name):
self.warning.config(text='没有该景点')
self.warning.place(x=350, y=200)
else:
self.show_route()
def confirm(self):
self.confirm_butt.bind('<1>', self.press_confirm)
self.window.mainloop()
def show_route(self):
# 得到路线
js = json.dumps(
{
'type': "get route",
'msg1': str(self.start_name),
'msg2': str(self.terminal_name)
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if not jd:
route = (('故宫', '站一', '站二', '颐和园'),
('故宫', 'first', 'second', 'third', '颐和园'))
else:
route = jd['msg1']
bus_window = tk.Toplevel(self.window)
window_inter.WindowInter(bus_window, '景点信息', 500, 400).show()
# 打印滚动信息
scroll = tk.Scrollbar(bus_window)
scroll.pack(side="right", fill="y")
listbox = tk.Listbox(bus_window, yscrollcommand=scroll.set, font=("宋体", 12), width=70)
num = 0
count = 1
for i in route:
num = 0
listbox.insert("end", '第'+str(count)+'条路线:')
count += 1
for j in i:
listbox.insert("end", '第'+str(num)+'站: '+j)
num += 1
listbox.insert("end", ' ')
listbox.pack(side="left", fill="both")
scroll.config(command=listbox.yview)
button.py
import tkinter as tk
class EntryButton:
def __init__(self, window, text, show_type, distance):
self.text = text
self.showType = show_type
self.distance = distance
self.contain = tk.StringVar()
self.name = tk.Label(window, text=self.text)
self.contain = tk.Entry(
window, show=self.showType, textvariable=self.contain, font=('宋体', 14))
def show(self, x, y):
self.name.place(x=x, y=y)
self.contain.place(x=x + self.distance, y=y)
def get_contain(self):
return self.contain.get()
def get_contain_immediate(self):
return self.contain.get()
def forget(self):
self.name.place_forget()
self.contain.place_forget()
main.py
import tkinter as tk
import mainPage
window = tk.Tk()
mainPage.MainPage(window)
mainPage.py
import tkinter as tk
import user_interface
import window_inter
class MainPage:
def __init__(self, window):
self.window = window
window_inter.WindowInter(self.window, '主界面', 500, 400).show()
self.com = tk.Label(
self.window, text='欢迎来到\n\n北京市景点公交导览系统', bg='gray', font=('楷体', 20), padx=20, pady=10, relief="ridge")
self.com.place(x=70, y=10)
self.begin_but()
self.window.mainloop()
def begin(self):
self.window.destroy()
user_interface.UserInter()
def begin_but(self):
begin_but = tk.Button(self.window, text='点击开始', command=self.begin, font=('黑体', 20))
begin_but.place(x=165, y=200)
passinformation.py
import socket
import json
def pass_main(js):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.0.102', 8888))
s.send(js.encode())
while (True):
data = s.recv(2014).decode()
if data:
break
return data
rank.py
import tkinter as tk
import window_inter
import button
import json
class Rank:
def __init__(self, parent_window):
self.window = tk.Toplevel(parent_window)
window_inter.WindowInter(self.window, '景点热度排行榜', 500, 400).show()
self.contains = []
self.labels = []
self.get_rank()
# self.window.mainloop()
def get_rank(self):
"""从db中得到排行榜"""
js = json.dumps(
{
'type': 'get rank',
}
)
from passinfromation import pass_main
jk = pass_main(js)
jd = json.loads(jk)
if not jd:
self.contains = ("故宫", "second", "third", "forth", "颐和园")
else:
self.contains = jd['msg1']
scroll = tk.Scrollbar(self.window)
scroll.pack(side="right", fill="y")
listbox = tk.Listbox(self.window, yscrollcommand=scroll.set, font=("宋体", 12), width=70)
for i in range(len(self.contains)):
label = tk.Label(self.window, text=self.contains[i], font=("宋体", 15, "underline"))
self.labels.append(label)
listbox.insert("end", "第"+str(i)+":"+self.contains[i])
listbox.insert("end", " ")
listbox.pack(side="left", fill="both")
scroll.config(command=listbox.yview)
user_and_manager_choice.py
from this import s
import tkinter as tk
import button
import window_inter
import attractions
import bus_route
import rank
import account
import tkinter.messagebox
import user_interface
class Choice:
def __init__(self, user_name, pw, mode):
self.window = tk.Tk()
self.mode = mode # 0:user mode 1:manager mode
self.user_name = user_name
self.pw = pw
self.account_now = account.Account(self.user_name, self.pw)
# 用户模式下的变量
self.see_attract_butt = tk.Button(self.window, text='查看景点')
self.query_butt = tk.Button(self.window, text='公交路线查询')
self.rank_butt = tk.Button(self.window, text='排行榜')
self.see_attract_butt.bind('<1>', self.press_see_attract)
self.query_butt.bind('<1>', self.press_query)
self.rank_butt.bind('<1>', self.press_rank)
self.menubar = tk.Menu(self.window)
self.user_info = tk.Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label='账户', menu=self.user_info)
self.user_info.add_command(label='账户信息', command=self.account_info)
self.user_info.add_command(label='预约管理', command=self.appoint)
self.user_info.add_separator()
self.user_info.add_command(label='退出', command=self.quit)
# 管理员模式下的变量
self.tip = tk.Label(self.window, text="请选择登入身份:", font=("宋体", 15))
self.user_mode = tk.Button(self.window, text="用户", font=("宋体", 20))
self.user_mode.bind("<1>", lambda event: self.create_user_page())
self.manager_mode = tk.Button(self.window, text="管理员", font=("宋体", 20))
self.manager_mode.bind("<1>", lambda event: self.enter_manager_page())
# self.confirm()
def confirm(self):
if not self.mode:
self.create_user_page()
else:
self.create_manager_page()
self.window.mainloop()
def quit(self):
"""退出"""
warning = tkinter.messagebox.askokcancel('提示', '即将退出系统\n确定要执行此操作吗?')
if not warning:
self.create_user_page()
else:
self.window.destroy()
user_interface.UserInter()
"""用户 界面"""
def create_user_page(self):
window_inter.WindowInter(self.window, '欢迎用户 ' + str(self.user_name) + ' 进入系统', 500, 400).show()
self.tip.place_forget()
self.user_mode.place_forget()
self.manager_mode.place_forget()
self.see_attract_butt.place(x=100, y=40)
self.query_butt.place(x=100, y=140)
self.rank_butt.place(x=100, y=240)
self.window.config(menu=self.menubar)
"""用户 查看景点"""
def press_see_attract(self, event):
"""查看景点"""
attract = attractions.Attraction(self.user_name, self.pw, 0, self.window)
attract.confirm_view()
"""用户 查询公交路线"""
def press_query(self, event):
""""查询公交路线"""
query = bus_route.BusRoute(self.window)
query.confirm()
"""用户 查看排行榜"""
def press_rank(self, event):
"""排行榜"""
# self.window.destroy()
rank.Rank(self.window)
"""用户 账号信息"""
def account_info(self):
"""账户信息"""
print('account_info is clicked')
acc_window = tk.Toplevel(self.window)
window_inter.WindowInter(acc_window, '账户信息', 500, 400).show()
tk.Label(acc_window, text='账户名称:', font=("宋体", 15)).grid(row=2, column=1)
name = tk.Label(acc_window, text=self.user_name, font=("宋体", 15))
name.grid(row=2, column=3)
tk.Label(acc_window, text='注册时间:', font=("宋体", 15)).grid(row=6, column=1)
time = self.account_now.get_time()
time_label = tk.Label(acc_window, text=time, font=("宋体", 15))
time_label.grid(row=6, column=3)
cancellation = tk.Label(acc_window, text="注销", font=("宋体", 15, "underline"), fg="red")
cancellation.grid(row=10, column=1)
cancellation.bind("<1>", lambda event: self.press_cancel())
"""用户 注销账号"""
def press_cancel(self):
warning = tkinter.messagebox.askokcancel('提示', '账号会被注销\n确定要执行此操作吗?')
if warning:
self.account_now.cancellation()
tkinter.messagebox.showinfo('提示', '注销成功')
self.window.destroy()
user_interface.UserInter()
"""管理员 界面"""
def create_manager_page(self):
window_inter.WindowInter(self.window, '欢迎管理员 ' + str(self.user_name) + ' 进入系统', 500, 400).show()
self.tip.place(x=40, y=40)
self.user_mode.place(x=100, y=100)
self.manager_mode.place(x=200, y=100)
def enter_manager_page(self):
manager_window = tk.Toplevel(self.window)
window_inter.WindowInter(manager_window, '管理员系统', 500, 400).show()
manage_attraction = tk.Button(manager_window, text="景点管理", font=("宋体", 15))
manage_attraction.bind("<1>", lambda event: self.press_manage_attraction())
manage_user = tk.Button(manager_window, text="用户管理", font=("宋体", 15))
manage_user.bind("<1>", lambda event: self.press_manage_user())
statistical = tk.Button(manager_window, text="景点数据统计", font=("宋体", 15))
statistical.bind("<1>", lambda event: self.press_statistical(manager_window))
manage_attraction.grid(row=4, column=1)
manage_user.grid(row=4, column=7)
statistical.grid(row=4, column=13)
"""管理员 景点管理"""
def press_manage_attraction(self):
"""景点管理"""
attractions.Attraction(self.user_name, self.pw, 1, self.window).confirm_view()
"""管理员 预约管理"""
def appoint(self):
"""预约管理"""
app_window = tk.Toplevel(self.window)
window_inter.WindowInter(app_window, '预约信息', 500, 400).show()
adapt = []
appointments = self.account_now.get_appointment()
for i in range(len(appointments[0])):
tk.Label(app_window, text=appointments[0][i], font=("宋体", 15)).grid(row=i*2, column=1)
tk.Label(app_window, text=appointments[1][i], font=("宋体", 15)).grid(row=i*2, column=3)
adapt.append(tk.Label(app_window, text='删除', font=('黑体', 16, "underline"), fg="red"))
adapt[i].grid(row=i*2, column=5)
adapt[i].bind("<1>", lambda event: self.press_delete_appoint(
appointments[0][i], appointments[1][i], app_window))
def press_delete_appoint(self, attr_name, time, window):
msg = tkinter.messagebox.askokcancel("警告", "确定删除该预约?")
if msg:
# 从db中删除景点预约
account.Account(self.user_name, self.pw).cancel_appointment(attr_name=attr_name, appoint_time=time)
tkinter.messagebox.showinfo('提示', '删除成功')
window.destroy()
self.appoint()
return "break"
"""管理员 用户管理"""
def press_manage_user(self):
"""用户管理"""
manage_window = tk.Toplevel(self.window)
window_inter.WindowInter(manage_window, '用户管理', 500, 400).show()
statistic_butt = tk.Button(manage_window, text='用户统计', font=("宋体", 20))
statistic_butt.place(x=100, y=40)
statistic_butt.bind("<1>", lambda event: self.press_statistic_butt(manage_window))
manage_account = tk.Button(manage_window, text='账号管理', font=("宋体", 20))
manage_account.place(x=100, y=140)
manage_account.bind("<1>", lambda event: self.press_manage_account(manage_window))
"""管理员 用户数据统计"""
def press_statistic_butt(self, parent_window):
statistic_window = tk.Toplevel(parent_window)
window_inter.WindowInter(statistic_window, '账号统计', 500, 400).show()
# 从db中获得总计用户数量
amount = 100
# 显示近三条注册记录
accounts = (("张三", "2022.1.1 11:00"),
("李四", "2022.2.2 22:00"),
("王麻子", "2022.3.3 13:00"))
# 打印滚动信息
scroll = tk.Scrollbar(statistic_window)
scroll.pack(side="right", fill="y")
listbox = tk.Listbox(statistic_window, yscrollcommand=scroll.set, font=("楷体", 15), width=70)
listbox.insert("end", '共计用户 '+str(amount)+' 人')
listbox.insert("end", ' ')
for i in range(len(accounts)):
listbox.insert("end", '用户 '+accounts[i][0]+' 注册时间: '+accounts[i][1])
listbox.pack(side="left", fill="both")
scroll.config(command=listbox.yview)
return "break"
"""管理员 账号管理"""
def press_manage_account(self, parent_window):
account_window = tk.Toplevel(parent_window)
window_inter.WindowInter(account_window, '账号管理', 500, 400).show()
entry_butt = button.EntryButton(account_window, '用户名: ', None, 60)
entry_butt.show(x=40, y=50)
confirm_butt = tk.Button(account_window, text='确认', font=("黑体", 14))
confirm_butt.place(x=300, y=50)
confirm_butt.bind("<1>", lambda event: self.confirm_the_account(entry_butt, account_window, parent_window))
return "break"
def confirm_the_account(self, entry_butt, window, parent_window):
warning = tk.Label(window, text='该账号不存在!', font=("黑体", 15), fg='red')
name = entry_butt.get_contain_immediate()
account_now = account.Account(name, -1)
if not account_now.is_in_db():
warning.place(x=50, y=100)
else:
warning.place_forget()
user_name_label = tk.Label(window, text='用户 '+name, font=("楷体", 15))
user_name_label.place(x=40, y=90)
user_register_time_label = tk.Label(window, text='注册时间: ' + account_now.get_time(), font=("楷体", 15))
user_register_time_label.place(x=40, y=130)
cancellation = tk.Label(window, text="注销", font=("黑体", 15, "underline"), fg="red")
cancellation.place(x=150, y=200)
cancellation.bind("<1>", lambda event: self.manager_press_cancel(
name, window, parent_window))
return "break"
"""管理员 注销用户账号"""
def manager_press_cancel(self, name, window, parent_window):
warning = tkinter.messagebox.askokcancel('提示', '账号会被注销\n确定要执行此操作吗?')
if warning:
account.Account(name, -1).cancellation()
tkinter.messagebox.showinfo('提示', '注销成功')
window.destroy()
# user_name_label.place_forget()
# user_register_time_label.place_forget()
self.press_manage_account(parent_window)
"""管理员 数据统计"""
def press_statistical(self, parent_window):
"""景点数据统计"""
window = tk.Toplevel(parent_window)
window_inter.WindowInter(window, '数据统计', 500, 400).show()
contains = ("区域景点记录", "景点开放情况", "票价统计")
area_attraction = tk.Button(window, text=contains[0], font=("楷体", 20))
area_attraction.bind("<1>", lambda event: self.show_statistic(contains[0], window))
open_condition = tk.Button(window, text=contains[1], font=("楷体", 20))
open_condition.bind("<1>", lambda event: self.show_statistic(contains[1], window))
price_of_all = tk.Button(window, text=contains[2], font=("楷体", 20))
price_of_all.bind("<1>", lambda event: self.show_statistic(contains[2], window))
area_attraction.place(x=40, y=10)
open_condition.place(x=40, y=100)
price_of_all.place(x=40, y=200)
def show_statistic(self, title, parent_window):
window = tk.Toplevel(parent_window)
window_inter.WindowInter(window, title, 500, 400).show()
attraction = attractions.Attraction(self.user_name, self.pw, 1, window)
attraction.get_attraction_window().withdraw()
# 打印滚动信息
scroll = tk.Scrollbar(window)
scroll.pack(side="right", fill="y")
listbox = tk.Listbox(window, yscrollcommand=scroll.set, font=("宋体", 12), width=70)
if title == "区域景点记录":
""" contains = (("朝阳区", "1234"),
("天河区", "4321"),
("xx区", "1111"))
"""
details = attraction.get_area_attraction()
for i in range(len(details)):
listbox.insert("end", details[i][0]+':'+details[i][0])
elif title == "景点开放情况":
"""
contains = ("111", "222") # 开放,未开放
"""
details = attraction.get_open_condition()
listbox.insert("end", "开放景点: "+details[0])
listbox.insert("end", "未开放景点: " + details[1])
elif title == "票价统计":
"""
contains = (("0 - 10", "10"),
("10 - 50", "11"),
("50 - 100", "12"),
("100以上", "13"))
"""
details = attraction.get_price_of_all()
for i in range(len(details)):
listbox.insert("end", '票价 '+details[i][0]+' 的景点个数: '+details[i][1])
listbox.pack(side="left", fill="both")
scroll.config(command=listbox.yview)
user_interface.py
import tkinter as tk
import user_login
import user_regist
import window_inter
class UserInter:
def __init__(self):
self.window = tk.Tk()
window_inter.WindowInter(self.window, '北京市景点公交导览系统登录', 500, 400).show()
login_but = tk.Button(
self.window, text='登录', command=self.login, padx=3, pady=2, font=('黑体', 20))
regist_but = tk.Button(
self.window, text='注册', command=self.regist, padx=3, pady=2, font=('黑体', 20))
login_but.place(x=160, y=100)
regist_but.place(x=160, y=200)
self.window.mainloop()
def login(self):
self.window.destroy()
login = user_login.LogPage()
login.confirm()
def regist(self):
self.window.destroy()
register = user_regist.RegistPage()
register.confirm()
user_login.py
import tkinter as tk
import button
import user_interface
import window_inter
import user_and_manager_choice
import account
class LogPage:
""""用户登录"""
def __init__(self):
self.window = tk.Tk()
window_inter.WindowInter(self.window, '用户登录', 500, 400).show()
self.user = button.EntryButton(self.window, '用户名', None, 60)
self.user.show(40, 100)
self.pw = button.EntryButton(self.window, '密码', '*', 60)
self.pw.show(40, 180)
self.confirm_butt = tk.Button(self.window, text='确认', font=15)
self.confirm_butt.place(x=100, y=240)
self.return_butt = tk.Button(self.window, text='返回', font=15)
self.return_butt.place(x=220, y=240)
self.warning = tk.Label(self.window, text='')
def press_confirm(self, event):
account_now = account.Account(self.user.get_contain(), self.pw.get_contain())
if not self.user.get_contain():
self.warning.config(text='账号不能为空')
self.warning.place(x=330, y=90)
elif not self.pw.get_contain():
self.warning.config(text='密码不能为空')
self.warning.place(x=330, y=180)
elif not account_now.is_in_db():
self.warning.config(text='该账号不存在')
self.warning.place(x=330, y=90)
elif not account_now.is_pw_correct():
self.warning.config(text='密码错误')
self.warning.place(x=330, y=180)
elif not account_now.is_manager():
user_name = self.user.get_contain()
pw = self.pw.get_contain()
self.window.destroy()
choice = user_and_manager_choice.Choice(user_name, pw, 0).confirm()
elif account_now.is_manager():
user_name = self.user.get_contain()
pw = self.pw.get_contain()
self.window.destroy()
choice = user_and_manager_choice.Choice(user_name, pw, 1).confirm()
return "break"
def press_return(self, event):
self.window.destroy()
user_interface.UserInter()
return "break"
def confirm(self):
self.confirm_butt.bind('<1>', self.press_confirm)
self.return_butt.bind('<1>', self.press_return)
self.window.mainloop()
user_regist.py
import tkinter as tk
import tkinter.messagebox
import button
import user_interface
import window_inter
import account
class RegistPage:
def __init__(self):
self.window = tk.Tk()
window_inter.WindowInter(self.window, '用户注册', 500, 400).show()
self.user = button.EntryButton(self.window, '用户名', None, 60)
self.user.show(40, 40)
self.pw_1 = button.EntryButton(self.window, '密码', '*', 55)
self.pw_1.show(45, 100)
self.pw_2 = button.EntryButton(self.window, '确认密码', '*', 70)
self.pw_2.show(30, 160)
self.confirm_butt = tk.Button(self.window, text='确认')
self.confirm_butt.place(x=100, y=240)
self.return_butt = tk.Button(self.window, text='返回')
self.return_butt.place(x=220, y=240)
self.warning = tk.Label(self.window, text='')
# self.window.mainloop()
def is_correct(self):
"""双重密码确认"""
if self.pw_1.get_contain() == self.pw_2.get_contain():
return True
else:
return False
def press_confirm(self, event):
account_now = account.Account(self.user.get_contain(), self.pw_1.get_contain())
if self.user.get_contain() == '':
self.warning.config(text='用户名不能为空')
self.warning.place(x=320, y=40)
elif self.pw_1.get_contain() == '':
self.warning.config(text='密码不能为空')
self.warning.place(x=320, y=100)
elif not self.is_correct():
self.warning.config(text='密码不一致')
self.warning.place(x=320, y=155)
elif account_now.is_in_db():
self.warning.config(text='该账号已存在')
self.warning.place(x=100, y=40)
else:
account_now.create_account()
self.warning.place_forget()
tkinter.messagebox.showinfo(title='提示', message='创建成功')
return "break"
def press_return(self, event):
self.window.destroy()
user_interface.UserInter()
return "break"
def confirm(self):
self.confirm_butt.bind('<1>', self.press_confirm)
self.return_butt.bind('<1>', self.press_return)
self.window.mainloop()
window_inter.py
import tkinter as tk
# from PIL import ImageTk, Image
class WindowInter:
def __init__(self, window, title, width, height):
self.window = window
self.width = width
self.height = height
self.window.title(title)
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
# image2 = Image.open(r'C:\Python27\tcl\tk8.5\demos\images\earth.gif')
# background_image = ImageTk.PhotoImage(image2)
self.left = (screen_width - self.width) / 2
self.top = (screen_height - self.height) / 2
def show(self):
self.window.geometry("%dx%d+%d+%d" % (self.width, self.height, self.left, self.top))