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
  [PyQt] Embed Google Maps in PyQt6 Widget Raures 2 684 Sep-11-2021, 04:32 PM
Last Post: Raures
  Embed Python console in GUI application deanhystad 5 4,369 Jun-04-2021, 05:10 PM
Last Post: deanhystad
  [Tkinter] Frame with treeview virencm 3 1,218 May-26-2021, 04:36 AM
Last Post: virencm
  Treeview - SQL - Add child node snakes 0 754 May-15-2021, 08:48 PM
Last Post: snakes
  tkinter - update/refresh treeview snakes 4 5,483 May-13-2021, 07:10 AM
Last Post: snakes
  How to retrieve liststore iter when treeview row selected using sort and filter jimofmex 4 1,260 Apr-18-2021, 07:29 PM
Last Post: Axel_Erfurt
  Treeview scroll selected node to top rfresh737 1 896 Apr-14-2021, 03:27 AM
Last Post: deanhystad
Question [Tkinter] Scrollable Treeview: change behavior of Prior/Next keys? RockDoctor 2 1,192 Apr-10-2021, 05:40 PM
Last Post: RockDoctor
  [Tkinter] Justify the treeview to the last entry MrP 7 2,414 Mar-01-2021, 11:20 AM
Last Post: MrP
  [Tkinter] ttk-->treeview-->tag_configure() not work over Python3.6 hqbfljx 9 3,133 Jan-20-2021, 06:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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