The Treeview widget generalizes this concept so that you can use it to display any hierarchical structure, and the reader can collapse or expand subtrees of this structure with the mouse.
For a given item, if it is at the top of the hierarchy it is said to have no parent; if it is not at the top level, the parent is the item that contains it.
The descendants of an item include its children, its childrens‘ children, and so on. Another way of saying this is that the subtree of an item includes all its descendants.
Your program can associate one or more tag strings with each item. You can use these tags to control the appearance of an item. For example, you could tag directories with the tag ‘d‘ and files with the tag ‘f‘, and then specify that items with tag ‘d‘ use a boldface font.
You may also associate events with tags, so that certain events will cause certain handlers to be called for all items that have that tag. For example, you could set up a file browser so that when a user clicks on a directory, the browser updated its contents to reflect the current file structure.
Your Treeview widget will be structured with multiple columns. The first column, which we‘ll call the icon column, displays the icons that collapse or expand items. In the remaining columns, you may display whatever information you like.
nǐ你 de的 T r e e V i e w kòng控 jiàn件 de的 jié结 gòu构 jiāng将 yǔ与 duō多 gè个 liè列 。 wǒ我 men们 jiāng将 chēng称 wéi为 “ tú图 biāo标 ” liè列 de的 dì第 yī一 liè列 , xiǎn显 shì示 zhé折 dié叠 huò或 zhǎn展 kāi开 xiàng项 mù目 de的 tú图 biāo标 。 zài在 shèng剩 xià下 de的 lán栏 mù目 zhōng中 , nǐ你 kě可 yǐ以 zhǎn展 shì示 nǐ你 xǐ喜 huan欢 de的 rèn任 hé何 xìn信 xī息 。
For example, a simple file browser widget might use two columns, with the directory icons in the first column and the directory or file name in the second columns. Or you might wish to display file sizes, permissions, and other related data in additional columns.
The operations of the Treeview widget even allow you to use it as a tree editor. Your program can remove an entire subtree from its location in the main tree and then attach it later at an entirely different point.
zài在 T r e e V i e w kòng控 jiàn件 de的 cāo操 zuò作 , shèn甚 zhì至 yǔn允 xǔ许 nǐ你 shǐ使 yòng用 tā它 zuò作 wéi为 yī一 gè个 shù树 biān编 jí辑 。 nǐ你 de的 chéng程 xù序 kě可 yǐ以 shān删 chú除 zhěng整 gè个 zǐ子 shù树 cóng从 tā它 de的 wèi位 zhi置 zài在 zhǔ主 shù树 , rán然 hòu后 jiāng将 tā它 zhī之 hòu后 zài在 yī一 gè个 wán完 quán全 bù不 tóng同 de的 jiǎo角 dù度 。
Here is the general procedure for setting up a Treeview widget.
这里是设置TreeView控件的一般程序。
zhè这 lǐ里 shì是 shè设 zhì置 T r e e V i e w kòng控 jiàn件 de的 yī一 bān般 chéng程 xù序 。
1. Create the widget with the ttk.Treeview constructor. Use the columns keyword argument to specify the number of columns to be displayed and to assign symbolic names to each column.
1 。 chuàng创 jiàn建 T T K T r e e V i e w de的 gòu构 zào造 hán函 shù数 de的 bù部 jiàn件 。 shǐ使 yòng用 liè列 guān关 jiàn键 zì字 cān参 shù数 lái来 zhǐ指 dìng定 yào要 xiǎn显 shì示 de的 liè列 shǔ数 , bìng并 wèi为 měi每 gè个 liè列 fēn分 pèi配 fú符 hào号 míng名 。
2. Use the .column() and .heading() methods to set up column headings (if you want them) and configure column properties such as size and stretchability.
2 。 shǐ使 yòng用 。 c o l u m n ( ) hé和 。 h e a d i n g ( ) fāng方 fǎ法 shè设 zhì置 liè列 biāo标 tí题 ( rú如 guǒ果 nǐ你 xiǎng想 ) hé和 pèi配 zhì置 liè列 shǔ属 xìng性 rú如 dà大 xiǎo小 hé和 lā拉 shēn伸 xìng性 。
3. Starting with the top-level entries, use the .insert() method to populate the tree. Each call to this method adds one item to the tree. Use the open keyword argument of this method to specify whether the item is initially expanded or collapsed.
If you want to supply the iid value for this item, use the iid keyword argument. If you omit this argument, ttk will make one up and return it as the result of the .insert() method call.
rú如 guǒ果 nǐ你 xiǎng想 wèi为 zhè这 ge个 xiàng项 mù目 tí提 gōng供 de的 I D zhí值 , shǐ使 yòng用 I I D guān关 jiàn键 zì字 cān参 shù数 。 rú如 guǒ果 shěng省 lvè略 cǐ此 cān参 shù数 , jiāng将 shǐ使 yī一 gè个 T T K bìng并 fǎn返 huí回 tā它 de的 jié结 guǒ果 。 i n s e r t ( ) fāng方 fǎ法 diào调 yòng用 。
Use the values keyword argument of this method to specify what should appear in each column of this item when it is visible.
For example, if you specified columns=(‘Name‘, ‘Size‘), three columns would appear in the widget: first the icon column, then two more columns whose internal identifiers are ‘Name‘ and ‘Size‘.
For example, suppose you specify columns=(‘Name‘, ‘Size‘, ‘Date‘). This means each call to the .insert() method will require an argument values=(name, size, date) to supply the values that will be displayed. Let‘s call this sequence the logical column sequence.
lì例 rú如 , jiǎ假 shè设 nǐ你 zhǐ指 dìng定 liè列 = ( ‘ n a m e ‘ , ‘ dà大 xiǎo小 ‘ , ‘ rì日 ‘ ) 。 zhè这 yì意 wèi味 zhe着 měi每 gè个 diàn电 huà话 。 i n s e r t ( ) fāng方 fǎ法 xū需 yào要 yī一 gè个 cān参 shù数 zhí值 = ( míng名 chēng称 、 dà大 xiǎo小 、 rì日 qī期 ) tí提 gōng供 de的 zhí值 jiāng将 bèi被 xiǎn显 shì示 。 ràng让 wǒ我 men们 jiāng将 cǐ此 xù序 liè列 chēng称 wéi为 “ luó逻 jí辑 liè列 ” xù序 liè列 。
Further suppose that in the constructor you specify columns=(2,0). The physical column sequence, the columns that will actually appear in the widget, will be three: the icon column will be first, followed by the date column (index 2 in the logical column sequence), followed by the name column (logical column index 0). The size column will not appear.