Python Forum
[Tkinter] Treeview actions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Treeview actions
#1
So I have been trying to learn a bit of tkinter by reading documentation and watching some videos. I have created a little price list program. It fills a treeview with information from a SQLite file.

I was wondering if there is an action where if I click on one of the lines in the treeview, it loads those values into some text boxes. I would like to load it into the text boxes so that the values can be changed and updated. I have the function working, but right now they have to type the info into the boxes, I thought it would be smoother if they could just click the item in the tree it loads it to the boxes, they make the changes and click the update button.

Is this possible or do I need to use a different widget, or even have to go to like QtPy or something?

TIA
Reply
#2
Bind a '<ButtonRelease-1>' event to the treeview.
In the event handler do the following:
use the treeview's focus method to get the iid that has focus.
Use the treeview's item method, passing it the iid and it will return a dictionary.
Use the dictionary to update the relevant widget.
Reply
#3
(Oct-23-2022, 09:14 AM)Yoriz Wrote: Bind a '<ButtonRelease-1>' event to the treeview.
In the event handler do the following:
use the treeview's focus method to get the iid that has focus.
Use the treeview's item method, passing it the iid and it will return a dictionary.
Use the dictionary to update the relevant widget.


Exactly the direction I was looking for! I will research those events and methods, thank you so much!!
Reply
#4
ok, this ended up being a lot easier than I thought it would be. If anyone searches this, here is what I did.

#bind single click event to treeview and call function that sets values.
self.tree.bind("<ButtonRelease-1>", self.select_record)

#clears all text boxes (e1-e6 are textbox items)
def clear_entries(self):
self.e1.delete(0, 'end')
self.e2.delete(0, 'end')
self.e3.delete(0, 'end')
self.e4.delete(0, 'end')
self.e5.delete(0, 'end')
self.e6.delete(0, 'end')
# call the clear-entry function, then use focus method to select which item in the tree. call item function and throw values into a list. then
# set the values from the list into the e1-e6 text boxes. remember to pass the event "e"(or whatever you want to name it) this is the only thing that caught me up

def select_record(self,e):
self.clear_entries()
entry = self.tree.focus()
values = self.tree.item(entry, "values")
self.e1.insert(0, values[0])
self.e2.insert(0, values[1])
self.e3.insert(0, values[2])
self.e4.insert(0, values[3])
self.e5.insert(0, values[4])
self.e6.insert(0, values[5])

Thanks again Yoriz for pointing me in the right direction.
Reply
#5
Have you run across tkinter variables? I think they are much easier to use than directly working with widgets. In the example below I use StringVars to set values in the Entry widgets.

If you have a bunch of related variables, make that obvious by putting them all in a list,
class MyWindow(tk.Tk):
    def __init__(self):
        super().__init__()

        frame = tk.Frame(self)
        frame.pack(expand=True, fill=tk.BOTH)
        self.dice = [tk.StringVar(self, "") for _ in range(5)]
        for die in self.dice:
            entry = tk.Entry(frame, textvariable=die, width=3, font=("Times", 24), justify=tk.CENTER)
            entry.pack(side=tk.LEFT, padx=2, pady=2, expand=True, fill=tk.BOTH)

        frame = tk.Frame(self)
        frame.pack(fill=tk.X)
        button = tk.Button(frame, text="Roll", command=self.roll)
        button.pack(side=tk.LEFT, padx=5, pady=5, expand=True, fill=tk.X)

        button = tk.Button(frame, text="Clear", command=self.clear)
        button.pack(side=tk.LEFT, padx=5, pady=5, expand=True, fill=tk.X)

    def roll(self):
        """ Roll dice and update display """
        for die in self.dice:
            die.set(str(randint(1, 6)))

    def clear(self):
        """ Clear all the dice faces """
        for die in self.dice:
            die.set(" ")

MyWindow().mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 3,301 Oct-20-2021, 12:14 AM
Last Post: CyKlop

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020