Search Booked Ticket

Booked Flight

Above UI we are going to create by Tkinter for Search Booked Ticket in our Flight management system desktop application. For that first we create a window and configure its title, size and color. Here we are creating a window at Top Level so we can add this window at the top of any other window.

    
        root = tk.Toplevel()
        # root = tk.Tk()
        root.title("Search Booked Ticket")

        root.configure(bg="#585858")
        root.resizable(width=0, height=0)
        win_width = 1280

        win_height = int(1280 * 56.25 / 100)
        root.geometry(str(win_width) + "x" + str(win_height))

        # Get screen size
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
        Util.set_font_size(screen_width, screen_height, win_width, win_height)

        self.width = win_width * 96 / 100
        self.height = win_height * 94 / 100
        cornerradius = 40
        padding = 0
        self.color = "#FFFFFF"


       

    

After creation of window, we will add a canvas background.

    
    bg_canvas = RoundBackgroundFrame(self.root, self.width, self.height, padding, cornerradius, self.color,"#585858")
    bg_canvas.place(width=self.width, height=self.height, x=win_width / 2 - self.width / 2, y=win_height / 2 - self.height / 2)
    
    class RoundBackgroundFrame(tk.Canvas):

        def __init__(self, parent, width, height, padding, cornerradius, color, bg):
            tk.Canvas.__init__(self, parent, borderwidth=0, relief="flat", highlightthickness=0, bg=bg)

            original = Image.open("images/corner.png") 
            resized = original.resize((int(40), int(40)), Image.ANTIALIAS) 
            self.image_r_t = ImageTk.PhotoImage(resized)

            original = original.rotate(90, expand=0)
            resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
            self.image_l_t = ImageTk.PhotoImage(resized)

            original = original.rotate(90, expand=0)
            resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
            self.image_l_b = ImageTk.PhotoImage(resized)

            original = original.rotate(90, expand=0)
            resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
            self.image_r_b = ImageTk.PhotoImage(resized)

            self.shape(width, height, padding, cornerradius, color)
            (x0, y0, x1, y1) = self.bbox("all")
            width = (x1 - x0)
            height = (y1 - y0)
            self.configure(width=width, height=height)

        def shape(self, width, height, padding, cornerradius, color):
            self.create_polygon((padding, height - cornerradius - padding, padding, cornerradius + padding,
                        padding + cornerradius, padding, width - padding - cornerradius, padding,
                        width - padding, cornerradius + padding, width - padding,
                        height - cornerradius - padding, width - padding - cornerradius, height - padding,
                        padding + cornerradius, height - padding), fill=color, outline=color)

            self.create_image(width - self.image_r_t.width(), 0, image=self.image_r_t, anchor=NW)
            self.create_image(0, 0, image=self.image_l_t, anchor=NW)
            self.create_image(0, height - self.image_l_b.width(), image=self.image_l_b, anchor=NW)
            self.create_image(width - self.image_r_b.width(), height - self.image_r_b.width(), image=self.image_r_b,anchor=NW)

    

Once your window is created with custom background, we will create a frame with some reduced dimensions.

    
        self.width = self.width * 98 / 100
        self.height = self.height * 96 / 100
        self.base_frame = Frame(self.root, width=self.width, height=self.height, bg=self.color)
        bg_canvas.create_window(self.width / 100, self.height * 2 / 100, anchor=NW, window=self.base_frame)



    # Add Heading Name
      add_heading_label(self.base_frame, self.color, AppConstant.FONT_SIZE, self.height, self.width)

      def add_heading_label(base_frame, color, font_size, height, width):
            label_heading = Label(base_frame, text="Booked Ticket", anchor=CENTER, bg=color,
                                font=("Lucida Grande", font_size + 4))
            label_heading.place(width=width * 90 / 100, height=height * 5 / 100, x=width * 5 / 100, y=height * 0.3 / 100)

    # Add line
    add_line_border(self.base_frame, self.color, self.height, self.width)

    def add_line_border(base_frame, color, height, width):
        line_canvas = Canvas(base_frame, bg=color, borderwidth=0, relief="flat", highlightthickness=0)
        line_canvas.place(width=width - 4, height=5, x=2, y=height * 10 / 100)
        line_canvas.create_line(0, 0, width, 0, fill="#787878")



    add_search_frame(self.base_frame, ("Lucida Grande", AppConstant.FONT_SIZE - 6), self.color, self.height,self.width)       
   
    def add_search_frame(base_frame, button_font, color, height, width):
        sfw = width * 0.98
        sfh = height * 0.87
        label_frame_search = LabelFrame(base_frame, text="Search", font=button_font, pady=sfw * 2 / 100,
                                        padx=sfw * 2 / 100, bg=color)
        label_frame_search.place(width=sfw, height=sfh, x=11, y=height * 0.12)

 



        
    

So we are done with all frame and background UI. Now we will add all the serach box.

    
    # Search table
        self.ce_booking_var = CustomEntry(self.base_frame, 300, 55, 10, 2, self.color, "Search Booked Ticket",
                                          "images/ic_search.png")
        self.ce_booking_var.place(x=91, y=self.height * 0.20)

        self.booking_var = tk.StringVar

        self.ce_booking_var.entry.bind('', self.search_booking)

        



        
    
             
  
    def search_booking(self, *arg):

        #   self.hide_edit_button()

        if self.ce_booking_var.entry.get() != "":
            self.tree.delete(*self.tree.get_children())
            conn = Util.connect_db()
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM `booking` WHERE `passenger_id` LIKE ? OR `booking_id` LIKE ? OR `flight_no` LIKE ?",
                           ('%' + str(self.ce_booking_var.entry.get()) + '%',
                            '%' + str(self.ce_booking_var.entry.get()) + '%', '%' + str(self.ce_booking_var.entry.get()) + '%'))
            fetch = cursor.fetchall()

            count = 0
            booked_ticket_data_list = []

            for data in fetch:
                cursor.execute(
                    'SELECT flight_name, route, departure_timing, arrival_timing FROM flight_detail where flight_no IS ?',
                    (data[1],))
                sql_output_two = cursor.fetchall()
                print(sql_output_two)
                cursor.execute(
                    'SELECT passenger_name, contact_number FROM passenger_detail where passenger_id IS ?',
                    (data[2],))
                sql_output_three = cursor.fetchall()

                print(sql_output_three)

                flight_tuple = (data[0],)
                passenger_tuple = (data[3],)
                # booked_ticket_data_list.append((flight_tuple))
                self.tree.insert('', 'end',
                                 values=flight_tuple + sql_output_two[0] + sql_output_three[0] + passenger_tuple)
                print(flight_tuple + sql_output_two[0] + sql_output_three[0] + passenger_tuple)
                count += 1

            # for data in fetch:
            #     if count % 2 == 0:
            #         self.tree.insert('', 'end', values=data, tags=('even',))
            #     else:
            #         self.tree.insert('', 'end', values=data, tags=('odd',))
            #     count = count + 1

            # for data in fetch:
            #     self.tree.insert('', 'end', values=data)
            cursor.close()
            conn.close()
            else:
                self.reset_booked_ticket()

    def reset_booked_ticket(self):
        self.tree.delete(*self.tree.get_children())
        self.update_table_data()         

    
    

We have created a Database named "flight.db", lets connect that database and put that code in seperate class "Util" because we are going to use it multiple time in our project.

    
    class Util:

        @staticmethod
        def connect_db():
            db = None
            try:
                db = sql.connect("inventry.db")
                return db
            except sql.Error as error:
                print("Failed to insert data into sqlite table", error)
                

Here is our class CustomEntrySimple:

    
    class CustomEntry(tk.Canvas):

        def __init__(self, parent, width, height, cornerradius, padding, color, text, icon_path):
            tk.Canvas.__init__(self, parent, borderwidth=0, relief="flat", highlightthickness=0, bg=color)

            self.width = width
            self.height = height
            self.cornerradius = cornerradius
            self.padding = padding
            self.color = color

            original = Image.open(icon_path)
            resized = original.resize((int(height * 35 / 100), int(height * 35 / 100)), Image.ANTIALIAS)
            self.image = ImageTk.PhotoImage(resized)  # Keep a reference, prevent GC
            # self.color_text = "white"

            self.create_text(6, 7, anchor=W, font=font.Font(family="Lucida Grande", size=12, weight='bold'),
                            text=text, fill="#000000")
            self.create_image(10, height / 2 - self.image.width() / 2, image=self.image, anchor=NW)
            self.create_line(5, height - 8, width - 5, height - 8, fill="#808080")

            frame = Frame(parent,width=width * 86 / 100, height=height * 50 / 100, bg=color)
            # frame.place(x=0, y=0, width=100, height=50)
            self.entry = Entry(frame, bg=color, bd=0, highlightthickness=0,
                            font=font.Font(family="Lucida Grande", size=13, weight='normal'))

            self.entry.place(relwidth=1, relheight=1, x=0, y=0)

            self.create_window(width - width * 86 / 100 - 5, height / 2 - (height * 50 / 100) / 2, anchor=NW, window=frame)
            self.update()

            (x0, y0, x1, y1) = self.bbox("all")
            width = (x1 - x0)
            height = (y1 - y0)
            self.configure(width=width, height=height)
        

Here's the logic to Fetch data from database and show it in table form with the use of "Treeview" of Tkinter.

    
    # ===========Flight Tree ==============
    style = ttk.Style()
        # style.element_create("Custom.Treeheading.border", "from", "default")
        style.layout("Custom.Treeview.Heading", [
            ("Custom.Treeheading.cell", {'sticky': 'nswe'}),
            ("Custom.Treeheading.border", {'sticky': 'nswe', 'children': [
                ("Custom.Treeheading.padding", {'sticky': 'nswe', 'children': [
                    ("Custom.Treeheading.image", {'side': 'right', 'sticky': ''}),
                    ("Custom.Treeheading.text", {'sticky': 'we'})
                ]})
            ]}),
        ])
        #   style.configure("Custom.Treeview.Heading", background="blue", foreground="red", relief="flat")
        style.map("Custom.Treeview.Heading", relief=[('active', 'groove'), ('pressed', 'sunken')])
        style.configure("Custom.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11), rowheight=30)

        self.table_heading()

        self.tree = ttk.Treeview(self.base_frame, height=13, show='tree', style="Custom.Treeview")
        self.tree.tag_configure('odd', background='#fcd9e1', foreground="#000000", )
        self.tree.tag_configure('even', background='#FFFFFF', foreground="#000000", )
        self.tree.place(x=25, y=230)
        self.tree["columns"] = "1", "2", "3", "4", "5", "6", "7", "8"
        self.tree.column("#0", width=0)
        self.tree.column("#1", width=120)
        self.tree.column("#2", width=150)
        self.tree.column("#3", width=190)
        self.tree.column("#4", width=120)
        self.tree.column("#5", width=120)
        self.tree.column("#6", width=180)
        self.tree.column("#7", width=120)
        self.tree.column("#8", width=140)
        # count = 0
        # for data in self.sql_output:
        #     if count % 2 == 0:
        #         self.tree.insert('', 'end', values=data, tags=('even',))
        #     else:
        #         self.tree.insert('', 'end', values=data, tags=('odd',))
        #     count = count + 1

        self.get_table_data()
        vsb = ttk.Scrollbar(self.base_frame, orient="vertical", command=self.tree.yview)
        vsb.place(x=self.width - 40, y=230, height=395)

        self.tree.configure(yscrollcommand=vsb.set)

        self.tree.column("#1", anchor=tk.CENTER)
        self.tree.column("#2", anchor=tk.CENTER)
        self.tree.column("#3", anchor=tk.CENTER)
        self.tree.column("#4", anchor=tk.CENTER)
        self.tree.column("#5", anchor=tk.CENTER)
        self.tree.column("#6", anchor=tk.CENTER)
        self.tree.column("#7", anchor=tk.CENTER)
        self.tree.column("#8", anchor=tk.CENTER)


        root.attributes('-alpha', 0.0)
        Util.center(root)
        root.attributes('-alpha', 1.0)

       
    def get_table_data(self):
        conn = Util.connect_db()
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM booking')
        sql_output = cursor.fetchall()
        print(sql_output)
        count = 0
        booked_ticket_data_list = []
        booked_ticket_data_list2 = []
        for data in sql_output:
            cursor.execute(
                'SELECT flight_name, route, departure_timing, arrival_timing FROM flight_detail where flight_no IS ?', (data[1],))
            sql_output_two = cursor.fetchall()
            print(sql_output_two)
            cursor.execute(
                'SELECT passenger_name, contact_number FROM passenger_detail where passenger_id IS ?',
                (data[2],))
            sql_output_three = cursor.fetchall()

            print(sql_output_three)

            flight_tuple = (data[0],)
            passenger_tuple = (data[3],)
            # booked_ticket_data_list.append((flight_tuple))
            self.tree.insert('', 'end', values=flight_tuple + sql_output_two[0] + sql_output_three[0]+passenger_tuple)
            print(flight_tuple + sql_output_two[0] + sql_output_three[0]+ passenger_tuple)
            count += 1


                            
                            

When we search table , data will be data will show to database, after that we need to update the data on UI.

    
    def update_table_data(self):
        conn = Util.connect_db()
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM booking')
        sql_output = cursor.fetchall()
        print(sql_output)
        count = 0
        booked_ticket_data_list = []
        booked_ticket_data_list2 = []
        for data in sql_output:
            cursor.execute(
                'SELECT flight_name, route, departure_timing, arrival_timing FROM flight_detail where flight_no IS ?',
                (data[1],))
            sql_output_two = cursor.fetchall()
            print(sql_output_two)
            cursor.execute(
                'SELECT passenger_name, contact_number FROM passenger_detail where passenger_id IS ?',
                (data[2],))
            sql_output_three = cursor.fetchall()

            print(sql_output_three)

            flight_tuple = (data[0],)
            passenger_tuple = (data[3],)
            # booked_ticket_data_list.append((flight_tuple))
            self.tree.insert('', 'end', values=flight_tuple + sql_output_two[0] + sql_output_three[0] + passenger_tuple)
            print(flight_tuple + sql_output_two[0] + sql_output_three[0] + passenger_tuple)
            count += 1
    

We have created our own heading for table, lets configure it.

    
    def table_heading(self):

        heading_y = 203
        bg_color = "#f52f59"
        text_color = "#ffffff"

        canvas = Canvas(self.base_frame, width=1140, height=30)
        canvas.create_rectangle(0, 0, 1140, 30, fill=bg_color)
        canvas.place(x=25, y=heading_y - 3)

        booking_id_label = Label(self.base_frame, text="Booking Id",
                                 font=('Calibri', AppConstant.FONT_SIZE - 4, 'bold'), background=bg_color,
                                 foreground=text_color)
        booking_id_label.place(x=45, y=heading_y, width=120)
        booking_id_label.configure(anchor="center")

        flight_name_label = Label(self.base_frame, text="Flight Name",
                                   font=('Calibri', AppConstant.FONT_SIZE - 4, 'bold'), background=bg_color,
                                   foreground=text_color)
        flight_name_label.place(x=165, y=heading_y, width=150)
        flight_name_label.configure(anchor="center")

        flight_route_label = Label(self.base_frame, text="Flight Route",
                                      font=('Calibri', AppConstant.FONT_SIZE - 4, 'bold'), background=bg_color,
                                      foreground=text_color)
        flight_route_label.place(x=315, y=heading_y, width=180)
        flight_route_label.configure(anchor="center")

        dept_time_label = Label(self.base_frame, text="Dept. Time",
                                    font=('Calibri', AppConstant.FONT_SIZE - 4, 'bold'), background=bg_color,
                                    foreground=text_color)
        dept_time_label.place(x=495, y=heading_y, width=120)
        dept_time_label.configure(anchor="center")

        ara_time_label = Label(self.base_frame, text="Arv. Time",
                                    font=('Calibri', AppConstant.FONT_SIZE - 4, 'bold'), background=bg_color,
                                    foreground=text_color)
        ara_time_label.place(x=615, y=heading_y, width=120)
        ara_time_label.configure(anchor="center")

        passenger_name_label = Label(self.base_frame, text="Passenger Name",
                               font=('Calibri', AppConstant.FONT_SIZE - 4, 'bold'), background=bg_color,
                               foreground=text_color)
        passenger_name_label.place(x=735, y=heading_y, width=180)
        passenger_name_label.configure(anchor="center")

        passenger_contact_label = Label(self.base_frame, text="Contact No",
                                     font=('Calibri', AppConstant.FONT_SIZE - 4, 'bold'), background=bg_color,
                                     foreground=text_color)
        passenger_contact_label.place(x=915, y=heading_y, width=120)
        passenger_contact_label.configure(anchor="center")

        booking_date_label = Label(self.base_frame, text="Booking Date",
                                        font=('Calibri', AppConstant.FONT_SIZE - 4, 'bold'), background=bg_color,
                                        foreground=text_color)
        booking_date_label.place(x=1035, y=heading_y, width=130)
        booking_date_label.configure(anchor="center")

        

At last we will manage closing of window

    
    def on_closing():
                        
        root.destroy()
        args[0].deiconify()
                       
    root.protocol("WM_DELETE_WINDOW", on_closing)
                       

Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 2021 by Programmer Mitra. All Rights Reserved.