from tkinter import * import os import datetime win = Tk() win.title('火车订票系统') win.geometry('600x400+280+150') win.configure(bg='#f6677f') trainlist = [] people = [] m = '' s = '' class Train: def __init__(self): self.ID = '' self.firstname = '' self.lastname = '' self.time = '' self.price = 0 self.seating = 0 class Users: def __init__(self): self.personID = '' self.name = '' self.ID = '' self.time = '' self.seating = 0 def Init(trainlist): print("初始化......") file_object = open('train.txt', 'r') for line in file_object: tra = Train() line = line.strip("\n") s = line.split(" ") tra.ID = s[0] tra.firstname = s[1] tra.lastname = s[2] tra.time = s[3] tra.price = s[4] tra.seating = s[5] trainlist.append(tra) file_object.close() man_object = open('man.txt', 'r') for line in man_object: user = Users() line = line.strip("\n") m = line.split(" ") user.personID = m[0] user.name = m[1] user.ID = m[2] user.time = m[3] user.seating = m[4] people.append(user) man_object.close() print("初始化成功!") class ADD: def Add_f(self): roob = Toplevel(win) roob.title('火车信息输入') roob.geometry('500x240') lb1 = Label(roob, text='请输入车次号',bg='#d3fbfb') lb1.place(relx=0.1, rely=0.1, relwidth=0.5, relheight=0.1) a_text = StringVar() a_text.set("") self.inp1 = Entry(roob, textvariable=a_text) self.inp1.place(relx=0.6, rely=0.1, relwidth=0.3, relheight=0.1) lb2 = Label(roob, text='请输入火车起点',bg='#d3fbfb') lb2.place(relx=0.1, rely=0.2, relwidth=0.5, relheight=0.1) b_text = StringVar() b_text.set("") self.inp2 = Entry(roob, textvariable=b_text) self.inp2.place(relx=0.6, rely=0.2, relwidth=0.3, relheight=0.1) lb3 = Label(roob, text='请输入火车终点',bg='#d3fbfb') lb3.place(relx=0.1, rely=0.3, relwidth=0.5, relheight=0.1) c_text = StringVar() c_text.set("") self.inp3 = Entry(roob, textvariable=c_text) self.inp3.place(relx=0.6, rely=0.3, relwidth=0.3, relheight=0.1) lb4 = Label(roob, text='请输入火车出发时间',bg='#d3fbfb') lb4.place(relx=0.1, rely=0.4, relwidth=0.5, relheight=0.1) d_text = StringVar() d_text.set("") self.inp4 = Entry(roob, textvariable=d_text) self.inp4.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1) lb5 = Label(roob, text='请输入火车的票价',bg='#d3fbfb') lb5.place(relx=0.1, rely=0.5, relwidth=0.5, relheight=0.1) e_text = StringVar() e_text.set("") self.inp5 = Entry(roob, textvariable=e_text) self.inp5.place(relx=0.6, rely=0.5, relwidth=0.3, relheight=0.1) lb6 = Label(roob, text='请输入火车的票数',bg='#d3fbfb') lb6.place(relx=0.1, rely=0.6, relwidth=0.5, relheight=0.1) f_text = StringVar() f_text.set("") self.inp6 = Entry(roob, textvariable=f_text) self.inp6.place(relx=0.6, rely=0.6, relwidth=0.3, relheight=0.1) self.bt1 = Button(roob, text="清空数据",bg='#d3fbfb', command=self.Delete) self.bt1.place(relx=0.1, rely=0.9, relwidth=0.1, relheight=0.1) self.bt = Button(roob, text="添加",bg='#d3fbfb', command=lambda: self.Add(a_text, b_text, c_text, d_text, e_text, f_text)) self.bt.place(relx=0.7, rely=0.8, relwidth=0.2, relheight=0.1) self.btClose = Button(roob, text='关闭',bg='#d3fbfb', command=roob.destroy) self.btClose.place(relx=0.7, rely=0.9, relwidth=0.2, relheight=0.1) self.txt = Text(roob) self.txt.place(relx=0.1, rely=0.8, relwidth=0.4, relheight=0.1) def Delete(self): self.inp1.delete(0, END) self.inp2.delete(0, END) self.inp3.delete(0, END) self.inp4.delete(0, END) self.inp5.delete(0, END) self.inp6.delete(0, END) def Add(self, ID, firstname, lastname, time, price, seating): tra = Train() tra.ID = str(ID.get()) tra.firstname = str(firstname.get()) tra.lastname = str(lastname.get()) tra.time = str(time.get()) tra.price = str(price.get()) tra.seating = str(seating.get()) print(tra.ID, tra.firstname, tra.lastname, tra.time, tra.price, tra.seating) if self.searchByID(trainlist, tra.ID) == True: self.txt.delete(1.0, END) self.txt.insert(END, '车次重复,保存失败') return trainlist.append(tra) file_object = open("train.txt", "a") file_object.write(tra.ID) file_object.write(" ") file_object.write(tra.firstname) file_object.write(" ") file_object.write(tra.lastname) file_object.write(" ") file_object.write(tra.time) file_object.write(" ") file_object.write(tra.price) file_object.write(" ") file_object.write(tra.seating) file_object.write("\n") file_object.close() self.txt.delete(1.0, END) self.txt.insert(END, '保存成功') return def searchByID(self, trainlist, ID): for item in trainlist: if item.ID == ID: return True return False class BTT: def Btt_f(self): win_2 = Toplevel(win) win_2.title('用户订票') win_2.geometry('500x240') lb1 = Label(win_2, text='请输入出发地:',bg='#d3fbfb') lb1.place(relx=0, rely=0, relwidth=0.2, relheight=0.1) a_text = StringVar() a_text.set("") self.inp1 = Entry(win_2, textvariable=a_text) self.inp1.place(relx=0.2, rely=0, relwidth=0.3, relheight=0.1) lb2 = Label(win_2, text='请输入目的地:',bg='#d3fbfb') lb2.place(relx=0, rely=0.1, relwidth=0.2, relheight=0.1) b_text = StringVar() b_text.set("") self.inp2 = Entry(win_2, textvariable=b_text) self.inp2.place(relx=0.2, rely=0.1, relwidth=0.3, relheight=0.1) lb3 = Label(win_2, text='请输入您的姓名:',bg='#d3fbfb') lb3.place(relx=0, rely=0.65, relwidth=0.2, relheight=0.1) c_text = StringVar() c_text.set("") self.inp3 = Entry(win_2, textvariable=c_text) self.inp3.place(relx=0.2, rely=0.66, relwidth=0.3, relheight=0.07) lb4 = Label(win_2, text='请输入您的ID:',bg='#d3fbfb') lb4.place(relx=0, rely=0.75, relwidth=0.2, relheight=0.1) d_text = StringVar() d_text.set("") self.inp4 = Entry(win_2, textvariable=d_text) self.inp4.place(relx=0.2, rely=0.76, relwidth=0.3, relheight=0.07) lb5 = Label(win_2, text='请输入车次号:',bg='#d3fbfb') lb5.place(relx=0, rely=0.85, relwidth=0.2, relheight=0.1) e_text = StringVar() e_text.set("") self.inp5 = Entry(win_2, textvariable=e_text) self.inp5.place(relx=0.2, rely=0.86, relwidth=0.3, relheight=0.07) lb6 = Label(win_2, text='请输入所需票数:',bg='#d3fbfb') lb6.place(relx=0.6, rely=0.65, relwidth=0.2, relheight=0.1) f_text = StringVar() f_text.set("") self.inp6 = Entry(win_2, textvariable=f_text) self.inp6.place(relx=0.8, rely=0.66, relwidth=0.1, relheight=0.07) self.bt1 = Button(win_2, text="清空数据",bg='#d3fbfb', command=self.Delete) self.bt1.place(relx=0.6, rely=0.05, relwidth=0.15, relheight=0.1) self.btClose = Button(win_2, text='关闭',bg='#d3fbfb', command=win_2.destroy) self.btClose.place(relx=0.8, rely=0.05, relwidth=0.15, relheight=0.1) self.bt = Button(win_2, text="查询",bg='#d3fbfb', command=self.Search) self.bt.place(relx=0.27, rely=0.25, relwidth=0.15, relheight=0.1) self.bt2 = Button(win_2, text="确认",bg='#d3fbfb', command=lambda: self.Btt(c_text, d_text, e_text, f_text)) self.bt2.place(relx=0.7, rely=0.75, relwidth=0.15, relheight=0.1) self.txt = Text(win_2) self.txt.place(relx=0.05, rely=0.4, relwidth=0.85, relheight=0.2) self.txt_2 = Text(win_2) self.txt_2.place(relx=0.6, rely=0.9, relwidth=0.3, relheight=0.05) def Delete(self): self.inp1.delete(0, END) self.inp2.delete(0, END) self.inp3.delete(0, END) self.inp4.delete(0, END) self.inp5.delete(0, END) self.inp6.delete(0, END) self.txt.delete(1.0, END) def Search(self): firstname = self.inp1.get() lastname = self.inp2.get() if self.searchByID(trainlist, firstname) == False and self.searchByID2(trainlist, lastname) == False: self.txt.delete(1.0, END) self.txt.insert(END, '没有符合条件的火车!') self.inp1.delete(0, END) self.inp2.delete(0, END) return self.txt.delete(1.0, END) self.txt.insert(END, "{0:^6}\t{1:{6}^6}\t{2:{6}^6}\t{3:{6}^6}\t{4:{6}^6}\t{5:{6}^6}\n".format("车次", "起点", "终点", "时间", "票价", "剩余票数", chr(12288))) for item in trainlist: if item.firstname == firstname or item.lastname == lastname: self.txt.insert(END, "{0:^8}\t{1:{6}^6}\t{2:{6}^6}\t{3:^12}\t{4:^12}\t{5:^8}\n".format(item.ID, item.firstname, item.lastname, item.time, item.price, item.seating, chr(12288))) self.inp1.delete(0, END) self.inp2.delete(0, END) def searchByID(self, trainlist, firstname): for item in trainlist: if item.firstname == firstname: return True return False def searchByID2(self, trainlist, lastname): for item in trainlist: if item.lastname == lastname: return True return False def searchByID3(self, trainlist, ID): for item in trainlist: if item.ID == ID: return True return False def searchByID4(self, trainlist, ID, seating): for item in trainlist: if item.ID == ID: if int(item.seating) - int(seating) < 0: return True else: item.seating = str(int(item.seating) - int(seating)) return False def Btt(self, name, personID, ID, seating): man = Users() man.personID = str(personID.get()) man.name = str(name.get()) man.ID = str(ID.get()) for item in trainlist: if item.ID == ID.get(): man.time = str(item.time) man.seating = str(seating.get()) print(man.personID, man.name, man.ID, man.time, man.seating) if self.searchByID3(trainlist, man.ID) == False: self.txt_2.delete(1.0, END) self.txt_2.insert(END, '未找到对应火车! ') return if self.searchByID4(trainlist, man.ID, man.seating): self.txt_2.insert(END, '余票不足!') return people.append(man) man_object = open("man.txt", "a") man_object.write(man.personID) man_object.write(" ") man_object.write(man.name) man_object.write(" ") man_object.write(man.ID) man_object.write(" ") man_object.write(man.time) man_object.write(" ") man_object.write(man.seating) man_object.write("\n") man_object.close() self.txt_2.delete(1.0, END) self.txt_2.insert(END, '订票成功') changeID = str(ID.get()) for item in trainlist: if item.ID == changeID: # trainlist.remove(item) file_object = open("train.txt", "w") for tra in trainlist: file_object.write(tra.ID) file_object.write(" ") file_object.write(tra.firstname) file_object.write(" ") file_object.write(tra.lastname) file_object.write(" ") file_object.write(str(tra.time)) file_object.write(" ") file_object.write(str(tra.price)) file_object.write(" ") file_object.write(str(tra.seating)) file_object.write("\n") file_object.close() return class DEL: def Del_f(self): roob = Toplevel(win) roob.title('火车信息删除') roob.geometry('500x240') lb4 = Label(roob, text='请输入火车车次号:') lb4.place(relx=0, rely=0.2, relwidth=0.3, relheight=0.1) d_text = StringVar() d_text.set("") self.inp4 = Entry(roob, textvariable=d_text) self.inp4.place(relx=0.3, rely=0.2, relwidth=0.2, relheight=0.1) self.bt = Button(roob, text="删除", command=self.Del) self.bt.place(relx=0.7, rely=0.2, relwidth=0.2, relheight=0.2) self.btClose = Button(roob, text='关闭', command=roob.destroy) self.btClose.place(relx=0.7, rely=0.4, relwidth=0.2, relheight=0.2) self.txt = Text(roob) self.txt.place(relx=0.05, rely=0.4, relwidth=0.5, relheight=0.2) def searchByID(self, trainlist, ID): for item in trainlist: if item.ID == ID: return True return False def Del(self): ID = self.inp4.get() if self.searchByID(trainlist, ID) == False: self.txt.delete(1.0, END) self.txt.insert(END, '没有该火车信息!') self.inp4.delete(0, END) return for item in trainlist: if item.ID == ID: trainlist.remove(item) self.txt.delete(1.0, END) self.txt.insert(END, '删除成功!') self.inp4.delete(0, END) break file_object = open("train.txt", "w") for tra in trainlist: file_object.write(tra.ID) file_object.write(" ") file_object.write(tra.firstname) file_object.write(" ") file_object.write(tra.lastname) file_object.write(" ") file_object.write(str(tra.time)) file_object.write(" ") file_object.write(str(tra.price)) file_object.write(" ") file_object.write(str(tra.seating)) file_object.write("\n") file_object.close() class PBE: def Pbe_f(self): roob = Toplevel(win) roob.title('个人订票查询') roob.geometry('500x240') lb4 = Label(roob, text='请输入您的ID:',bg='#d3fbfb') lb4.place(relx=0.1, rely=0.1, relwidth=0.2, relheight=0.2) d_text = StringVar() d_text.set("") self.inp4 = Entry(roob, textvariable=d_text) self.inp4.place(relx=0.3, rely=0.15, relwidth=0.3, relheight=0.1) self.bt = Button(roob, text="查询",bg='#d3fbfb', command=self.Search) self.bt.place(relx=0.75, rely=0.1, relwidth=0.15, relheight=0.15) self.btClose = Button(roob, text='关闭',bg='#d3fbfb', command=roob.destroy) self.btClose.place(relx=0.75, rely=0.3, relwidth=0.15, relheight=0.15) self.txt = Text(roob) self.txt.place(relx=0.1, rely=0.6, relwidth=0.8, relheight=0.2) def Search(self): personID = self.inp4.get() if self.searchByID(people, personID) == False: self.txt.delete(1.0, END) self.txt.insert(END, '没有订票信息!') self.inp4.delete(0, END) return self.txt.delete(1.0, END) self.txt.insert(END, "{0:^5}\t{1:{5}^10}\t{2:{5}^5}\t{3:{5}^8}\t{4:{5}^6}\n".format("ID", "姓名", "车次号", "时间", "票数", chr(12288))) for item in people: if item.personID == personID: self.txt.insert(END, "{0:^5}\t{1:{5}^10}\t{2:{5}^8}\t{3:{5}^8}\t{4:{5}^7}\n".format(item.personID, item.name, item.ID, item.time, item.seating, chr(12288))) self.inp4.delete(0, END) def searchByID(self, people, personID): for item in people: if item.personID == personID: return True return False class GRT: def Grt_f(self): roob = Toplevel(win) roob.title('个人退票') roob.geometry('400x160') lb4 = Label(roob, text='请输入您的个人ID:') lb4.place(relx=0, rely=0.05, relwidth=0.4, relheight=0.2) d_text = StringVar() d_text.set("") self.inp4 = Entry(roob, textvariable=d_text) self.inp4.place(relx=0.4, rely=0.07, relwidth=0.3, relheight=0.15) lb5 = Label(roob, text='请输入所退火车车次号:') lb5.place(relx=0, rely=0.25, relwidth=0.4, relheight=0.2) e_text = StringVar() e_text.set("") self.inp5 = Entry(roob, textvariable=e_text) self.inp5.place(relx=0.4, rely=0.27, relwidth=0.3, relheight=0.15) self.bt = Button(roob, text="退票", command=self.Del) self.bt.place(relx=0.6, rely=0.5, relwidth=0.3, relheight=0.2) self.btClose = Button(roob, text='关闭', command=roob.destroy) self.btClose.place(relx=0.6, rely=0.7, relwidth=0.3, relheight=0.2) self.txt = Text(roob) self.txt.place(relx=0.1, rely=0.6, relwidth=0.4, relheight=0.2) def searchByID(self, people, personID, trainID): for item in people: if item.personID == personID and item.ID == trainID: return True return False def searchByID4(self, trainlist, ID, seating): for item in trainlist: if item.ID == ID: item.seating = str(int(item.seating) + int(seating)) def Del(self): global changeID_2 personID = self.inp4.get() trainID = self.inp5.get() if not self.searchByID(people, personID, trainID): self.txt.delete(1.0, END) self.txt.insert(END, '没有订票信息!') self.inp4.delete(0, END) return for item in people: if item.personID == personID and item.ID == trainID: changeID_2 = item.ID self.searchByID4(trainlist, item.ID, item.seating) people.remove(item) self.txt.delete(1.0, END) self.txt.insert(END, '退票成功!') self.inp4.delete(0, END) break man_object = open("man.txt", "w") for man in people: man_object.write(man.personID) man_object.write(" ") man_object.write(man.name) man_object.write(" ") man_object.write(man.ID) man_object.write(" ") man_object.write(man.time) man_object.write(" ") man_object.write(str(man.seating)) man_object.write("\n") man_object.close() for item in trainlist: if item.ID == changeID_2: file_object = open("train.txt", "w") for tra in trainlist: file_object.write(tra.ID) file_object.write(" ") file_object.write(tra.firstname) file_object.write(" ") file_object.write(tra.lastname) file_object.write(" ") file_object.write(str(tra.time)) file_object.write(" ") file_object.write(str(tra.price)) file_object.write(" ") file_object.write(str(tra.seating)) file_object.write("\n") file_object.close() return def main(): b.destroy() win.title('火车订票系统') win.geometry('600x400+280+150') add = ADD() bt1 = Button(win, text="火车信息添加", command=add.Add_f,bg='#ff88ff') bt1.place(relx=0.35, rely=0.05, relwidth=0.3, relheight=0.15) dela = DEL() bt2 = Button(win, text="火车信息删除", command=dela.Del_f,bg='#eb77fa') bt2.place(relx=0.35, rely=0.20, relwidth=0.3, relheight=0.15) btt = BTT() bt3 = Button(win, text="个人订票", command=btt.Btt_f,bg='#fb66ff') bt3.place(relx=0.35, rely=0.35, relwidth=0.3, relheight=0.15) pbe = PBE() bt4 = Button(win, text="订票查询", command=pbe.Pbe_f,bg='#fe55ef') bt4.place(relx=0.35, rely=0.50, relwidth=0.3, relheight=0.15) grt = GRT() bt5 = Button(win, text="个人退票", command=grt.Grt_f,bg='#fe45ff') bt5.place(relx=0.35, rely=0.65, relwidth=0.3, relheight=0.15) bt6 = Button(win, text="退出系统", command=win.destroy,bg='#ff22ff') bt6.place(relx=0.35, rely=0.80, relwidth=0.3, relheight=0.15) Init(trainlist) b = Button(win, text="欢迎使用火车订票系统", bg='#f6677f', fg='#d3fbfb', font=('华文新魏', 32), relief=SUNKEN, command=main, width=500, height=240) b.pack() win.mainloop()