Analysis of code implementation for online flight booking system

Table of contents

statement

Problem explanation

一、No such file or directory: 'flight.txt'

二、-tkinter.TclError: couldn't recognize data in image file "light.jpg"

code analysis

def Add_f(self) in class ADD:

label function

layout button

def Delete(self) in class ADD:


statement

Based on a previous article - online airplane booking system python simulated airplane booking system_airplane booking system python_Hansu Luo Beiwan's blog-CSDN blog

Do some problem explanation, code analysis and system optimization here

Problem explanation

一、No such file or directory: 'flight.txt'

The reason for this situation should be that the txt file and the py file are not in the same root directory.

Solution: Put the txt file and py file in a folder

二、-tkinter.TclError: couldn't recognize data in image file "light.jpg"

The reason for this situation is that the tkinter library cannot recognize files in .jpg format

Solution: Change jpg to gif file, or use a library, such as PIL library

code analysis

The entire program defines 10 classes, namely

Flight category

class Train:  # 定义一个航班类
    def __init__(self):
        self.ID = ''
        self.firstname = ''
        self.lastname = ''
        self.time = ''
        self.price = 0
        self.seating = 0

User class

class Users:
    def __init__(self):
        self.personID = ''
        self.name = ''
        self.ID = ''
        self.time = ''
        self.seating = 0

Each of the remaining eight functional modules is a class. In each class, you can define the functions you need to write to jointly implement the functions of each module.

Explain a module

class ADD:  # 信息录入
    def Add_f(self):
        roob = Toplevel(win)
        roob.title('飞机信息输入')
        roob.geometry('500x240')
 
        lb1 = Label(roob, text='请输入航班号')
        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='请输入起点')
        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='请输入终点')
        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='请输入飞机起飞时间')
        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='请输入飞机的票价')
        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='请输入飞机的票数')
        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="清空数据", command=self.Delete)
        self.bt1.place(relx=0.1, rely=0.9, relwidth=0.1, relheight=0.1)
 
        self.bt = Button(roob, text="添加", 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='关闭', 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
 

def Add_f(self) in class ADD:

For example, in the class ADD module on line 58, def Add_f(self): can add information.

    def Add_f(self):
        roob = Toplevel(win)
        roob.title('飞机信息输入')
        roob.geometry('500x240')
 
        lb1 = Label(roob, text='请输入航班号')
        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='请输入起点')
        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='请输入终点')
        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='请输入飞机起飞时间')
        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='请输入飞机的票价')
        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='请输入飞机的票数')
        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="清空数据", command=self.Delete)
        self.bt1.place(relx=0.1, rely=0.9, relwidth=0.1, relheight=0.1)
 
        self.bt = Button(roob, text="添加", 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='关闭', 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)

label function

The following lines of code are used to set the buttons on the interface using the label function. For specific methods of using the label function, please see Python Tkinter Detailed Explanation (2) Use of Label Labels_label in python_Maker Teacher Apple's Blog-CSDN Blog

        lb1 = Label(roob, text='请输入航班号')
        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)

By analogy, the following buttons are all implemented by the label function

layout button

lb1.place(relx=0.1, rely=0.1, relwidth=0.5, relheight=0.1)

This line of code is for laying out buttons. For specific relwidth and the like, please see Tkinter Layout Manager (detailed explanation of three methods) (biancheng.net)

def Delete(self) in class ADD:

    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)

Just use the delete function for this. For details, see Text.delete_tkinter text delete_WeChat-Alipay's blog-CSDN blog in tkinter programming in #Python3

Others can be deduced in this way

Guess you like

Origin blog.csdn.net/m0_74776728/article/details/131317856