Python Forum
Problem in list manipulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in list manipulation
#7
(Oct-17-2021, 04:41 PM)CyKlop Wrote: The purpose of BindableList is to keep a tkinter.ttk.Treeview synchronized with the various lists, even if the list is modified directly by someone else (i.e. a variation on the observer design pattern).

I made an example, but without binding events. If you want to copy the behavior of a list, don't use a list.
Instead you can inherit your class from collections.UserList and work with tkinter.ttk.TreeView as composition.
The problem is, that list (UserList) and TreeView do have methods with equal names.

Here the demo code:
import operator
import random
import time
from collections import UserList
from tkinter import END, Tk
from tkinter.ttk import Treeview


class ListTreeView(UserList):
    def __init__(self, master=None, columns=None):
        super().__init__()
        self.master = master
        self.treeview = Treeview(master, columns=columns, show="headings")

    def append(self, data):
        super().append(data)
        self.treeview.insert("", END, values=data)

    def extend(self, iterable):
        super().extend(iterable)
        for item in iterable:
            self.treeview.insert("", END, values=item)

    def remove(self, item):
        index = self.index(item)
        del self[index]
        selector = operator.itemgetter(index)
        children = self.treeview.get_children()
        self.treeview.delete(selector(children))

    def grid(self, *args, **kwargs):
        return self.treeview.grid(*args, **kwargs)

    def pack(self, *args, **kwargs):
        return self.treeview.pack(*args, **kwargs)

    def __delitem__(self, index):
        treeview_index = self.treeview.get_children()
        super().__delitem__(index)
        self.treeview.delete(treeview_index[index])


if __name__ == "__main__":
    root = Tk()
    tv = ListTreeView(root, ["index", "random"])
    tv.pack()

    for index in range(100, 106):
        time.sleep(0.2)
        tv.append([index, random.randint(0, 100)])
        root.update()

    # delete last
    time.sleep(2)
    del tv[-1]
    root.update()

    # delete first
    time.sleep(1)
    del tv[0]
    root.update()

    time.sleep(1)

    root.destroy()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Problem in list manipulation - by CyKlop - Oct-17-2021, 04:41 PM
RE: Problem in list manipulation - by bowlofred - Oct-17-2021, 09:00 PM
RE: Problem in list manipulation - by CyKlop - Oct-17-2021, 09:54 PM
RE: Problem in list manipulation - by deanhystad - Oct-18-2021, 03:42 AM
RE: Problem in list manipulation - by CyKlop - Oct-18-2021, 04:27 AM
RE: Problem in list manipulation - by deanhystad - Oct-18-2021, 08:44 AM
RE: Problem in list manipulation - by DeaD_EyE - Oct-18-2021, 09:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with "Number List" problem on HackerRank Pnerd 5 2,223 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  optimization problem for dataframe manipulation fimmu 0 1,520 Aug-31-2020, 06:02 PM
Last Post: fimmu
  How to pass a list by value for manipulation within a process? bRitch022 4 2,824 Jul-09-2020, 07:13 PM
Last Post: bRitch022
  IP string manipulation problem TheRealNoob 12 7,550 Feb-04-2019, 09:29 AM
Last Post: perfringo
  list manipulation cameronwood611 3 3,664 Oct-03-2017, 02:58 PM
Last Post: ichabod801
  list or dictionary manipulation dtigue 5 123,392 Jul-21-2017, 03:14 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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