Home > Software design >  How to open a window by clicking on a column in a tree view table?
How to open a window by clicking on a column in a tree view table?

Time:12-01

How would you double click on a column in a tree view table to then display the specific records on entry fields on a new window.

def SearchCustomer(self):
            connection = sqlite3.connect("Guestrecord.db")
            cursor = connection.cursor()

            columnID = ["GuestID","title","firstName","surname","dob","payment","email","phoneno","address","postcode"]
            columnStr =["GuestID","Title","FirstName","Surname","DOB","Payment","Email","PhoneNo","Address","Postcode"]

            self.search_table = ttk.Treeview(self.search_frame,columns=columnID,show="headings")

##            self.search_table.bind("<Motion>","break")

            for i in range(0,10):
                self.search_table.heading(columnID[i],text = columnStr[i])
                self.search_table.column(columnID[i],minwidth = 0, width = 90)
            self.search_table.place(x=0,y=0)

            for GuestRec in cursor.execute("SELECT * FROM tb1Guest1"):
                self.search_table.insert("",END,values=GuestRec)

            self.search_table.bind("<Double-1>", self.OnDoubleClick)

            connection.commit()
            connection.close()
        SearchCustomer(self)
        sqlCommand = """
           CREATE TABLE IF NOT EXISTS tb1Guest1
           (
           guestID INTEGER NOT NULL,
           guestTitle TEXT,
           guestFirstname TEXT,
           guestSurname TEXT,
           guestDOB DATE,
           guestPaymentType TEXT,
           guestEmail TEXT,
           guestPhoneNumber INTEGER,
           guestAddress TEXT,
           guestPostcode TEXT,
           primary key (guestID)
           )
        """

        self.search_firstname = Entry(self.search_frame2, width=25,bg="#e2f0d9",font=("Avenir Next",18),highlightthickness = 0,relief=FLAT)
        self.search_firstname.place(x = 140, y =0)
        self.search_firstname_label = Label(self.search_frame2,bg = "white", text = "First Name", font=("Avenir Next",20))
        self.search_firstname_label.place(x= 30,y=0)
        self.search_Surname = Entry(self.search_frame2, width=25,bg="#e2f0d9",font=("Avenir Next",18),highlightthickness = 0,relief=FLAT)
        self.search_Surname.place(x = 540, y =0)
        self.search_Surname_label = Label(self.search_frame2,bg = "white", text = "Surname", font=("Avenir Next",20))
        self.search_Surname_label.place(x= 450,y=0)
    


##      Binding entries
        self.search_firstname.bind("<KeyRelease>",self.Search)
        self.search_Surname.bind("<KeyRelease>",self.Search)

    def OnDoubleClick(self, event):
        self.gf_window.destroy()
        self.search_results()

This is the my code for displaying my records in my search tree view table. I am able to double click on a column to then bring up a new window but I am unsure how I would display the specific records on my entry fields I am making on the new window? If anyone could suggest a solution to this torment it would be much amazing. Thanks in advance.

CodePudding user response:

You should be able to grab the selected Treeview item from the mouse event being passed to your OnDoubleClick() function. If you want to open a new window that's a child of your root window, you'll want a Toplevel widget. You can treat that Toplevel window pretty much just like you would a root Tk window.

# side note: this should really be named "on_double_click" by convention
def OnDoubleClick(self, event):  
    tree = event.widget  # get the treeview widget
    region = tree.identify_region(event.x, event.y)  # get click location
    iid = tree.identify('item', event.x, event.y)  # get item ID

    if region == 'cell':  # i.e., if the click wasn't on the header row...
        data = tree.item(iid)['values']  # get the item data
        window = Toplevel(self)
        window.transient(self)  # optional: hide window controls except [x], pull focus
        # whatever you want here...
        window.geometry = '400x600'
        window.title('Results')
        label = ttk.Label(window, text=data)
        label.pack()
        # yadda yadda...
  • Related