Python Forum
[Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview?
#1
Now comes the next question, this time regarding tkinter.ttk.Treeview

Is there a way to embed a treeview as a row inside another treeview? The reason I ask is that the structures I want to show inside the (outer) treeview look like this:

+---------------+--------------+-------------------+
|Project Name   | Start Date   |                   |
+---------------+--------------+                   |
|[+] Foo        | 2021/10/10   |                   |
|    +-----------------+--------+----------------+ |
|    | Name            | Emp ID | Department     | |
|    +-----------------+--------+----------------+ |
|    | Marlon Brando   | 00001  | Public Affairs | |
|    | James Dean      | 00002  | Accounting     | |
|    | Elvis Presely   | 01467  | Liasion        | |
|    +-----------------+--------+----------------+ |
|[+] Bar        | 2021/12/25   |                   |
|    +-----------------+--------+----------------+ |
|    | Name            | Emp ID | Department     | |
|    +-----------------+--------+----------------+ |
|    | Charlie Chaplin | 02001  | Public Affairs | |
|    | Larry           | 02002  | Woodshop       | |
|    | Curly           | 02467  | Painting       | |
|    +-----------------+--------+----------------+ |
etc
It appears that Treeview can only handle homogenous rows, unfortunately. I know I could do it like this:

+---------------+------------+---------------+--------+----------------+
| Project Name  | Start Date | Name          | Emp ID | Department     |
|[+] Foo        | 2021/10/10 |               |        |                |
|               |            | Marlon Brando | 00001  | Public Affairs |
|               |            | James Dean    | 00002  | Accounting     |
|               |            | Elvis Presely | 01467  | Liasion        |
etc
but that doesn't work well if there are more than a couple of fields for both the outer and subordinate rows.

I guess this is more a tkinter question than a python question, but I'm hoping someone can give me a start at the above -- much as you all straighened out my misapprehension about the list.remove() thing (which is solved, by the way -- thank you!)
Reply
#2
I've been going through the documentation and I'm starting to think I'll have to do some sort of custom widget to do this, darn it :/
Reply
#3
Take a look at: https://stackoverflow.com/a/57039335
Is this what you are trying to do?
Reply
#4
(Oct-19-2021, 03:33 PM)Larz60+ Wrote: Take a look at: https://stackoverflow.com/a/57039335
Is this what you are trying to do?

Can't quite say. Assume I have two classes:

# yeah, I know these should be dataclasses.  This is just a fast example.
class Member:
    def __init__(self, *, name: Optional[str] = None, office: Optional[Position] = None, joined: Optional[datetime] = None, dues_paid: bool = False, phone: Optional[str] = None, address: Optional[str] = None, city: Optional[str] = None, province: Optional[str] = None):
        self.name = name
        self.office = office
        self.joined = joined
        self.dues_paid = dues_paid
        self.phone = phone
        self.address = address
        self.city = city
        self.province = province

class Chapter(list):
    def __init__(self, *, name: str, address: str, phone: str, officers: Optional[List[Member]] = None, members: Optional[List[Member]], founded: Optional[datetime] = None, motto: Optional[str], national_dues_paid: bool = False):
        self.name = name
        self.address = address
        self.phone = phone
        if officers is None:
            self.officers = list()
        else:
            self.officers = officers

        if members is None:
            self.members = list()
        else:
            self.members = members

        self.founded = founded
        self.motto = motto
        self.dues_paid = national_dues_paid

chapters = list() # Will hold a list of chapters
Note that the above is a fast hack done here in the editor to convey the flavour of what I want. It's not what I'm writing (which is a lot more complex) nor guaranteed to be working nor even good python-fu.

What I would like to do is display a treeview of chapters and their members, with all the data from a chapter being displayed. When the chapter is expanded, all the officers and then the members are displayed as a child of the chapter. Thus it would look something like:

--------------+--------+----------------+-----------------------+---------------------+------------------+
    Chapter   | Status | Phone Number   | Address               | City                | Country          |
--------------+--------+----------------+-----------------------+---------------------+------------------+
[+] Chapter 1 | Paid   | (555) 555-1212 | 1313 Mockingbird Lane | Monsterville        | Never-Never Land |

        ------------------+----------------+----------------+--------+--------------+-------------+
        Name              | Office         | Phone          | Status | Member Since | Country     |
        ------------------+----------------+----------------+--------+--------------+-------------+
        Peter Pan         | President      | (555) 555-1213 | Paid   | Dec 12, 1941 | Fantasyland |
        Tiger Lilly       | Vice-President | (555) 555-1214 | Paid   | Jul 20, 1969 | Fantasyland |
        ------------------+----------------+----------------+--------+--------------+-------------+

[+] Chapter 2 |        | (501) 555-1212 | P.O. Box 12727        | Langley, Virginia   | USA              |

        ------------------+----------------+----------------+--------+--------------+-------------+
        Name              | Office         | Phone          | Status | Member Since | Country     |
        ------------------+----------------+----------------+--------+--------------+-------------+
        Loch Ness Monster | President      | (555) 555-1215 | Paid   | Nov 11, 1918 | Loch Ness   |
        Bigfoot           |                | (555) 555-1216 |        | Sep 2, 1945  | Colorado    |
        Yeti              |                | (555) 555-1217 |        | Oct 4, 1957  | Tibet       |
        ------------------+----------------+----------------+--------+--------------+-------------+
--------------+--------+----------------+-----------------------+---------------------+------------------+
So, basically, it's a treeview where I want to show different column headings and column widths (and column formatting) for the child nodes (and a different number of columns)
Reply
#5
I am not used to seeing column headings for hierarchical information. I think that would be confusing.
Reply
#6
(Oct-19-2021, 11:32 PM)deanhystad Wrote: I am not used to seeing column headings for hierarchical information. I think that would be confusing.

The problem is, of course, that it is displaying a list, each item of which is ALSO a list, and the two lists are heterogenous (radically different shapes). Do you have a better idea how to display this? Although I've done some Python coding, I'm a neophyte at Tk. Perhaps there's a better widget for this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - update/refresh treeview snakes 5 20,822 Dec-02-2023, 07:05 PM
Last Post: aynous19
  Doubt approach update 2 Treeview at the same time TomasSanchexx 7 1,910 Sep-19-2023, 01:19 AM
Last Post: SaintOtis12
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 923 Aug-20-2023, 04:45 PM
Last Post: menator01
  [Tkinter] Search data in treeview without search button TomasSanchexx 3 1,589 Aug-12-2023, 03:17 AM
Last Post: deanhystad
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,356 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  [Tkinter] Different rows colours in treeview tkinter Krissstian 1 1,241 Nov-20-2022, 09:59 PM
Last Post: woooee
  [Tkinter] Treeview actions swanysto 4 4,826 Oct-24-2022, 08:19 PM
Last Post: deanhystad
  [Tkinter] ttk-->treeview-->tag_configure() not work over Python3.6 hqbfljx 10 8,373 Jul-04-2022, 02:40 PM
Last Post: jojyjoseph
  [Tkinter] Treeview heading event detect. water 1 2,220 Apr-10-2022, 11:06 PM
Last Post: Larz60+
  [Tkinter] Dynamic checkbox treeview issue OogieM 8 4,886 Mar-20-2022, 02:10 PM
Last Post: OogieM

Forum Jump:

User Panel Messages

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