猿问

Tkinter Treeview 如何用鼠标正确选择多个项目

我正在尝试使用鼠标选择和取消选择多个项目。我有它的工作方式,但是当用户快速移动鼠标时会出现问题。当鼠标快速移动时,一些项目被跳过并且根本没有被选中。我一定是走错路了。

更新 1: 我决定使用自己的选择系统,但得到的结果与上述相同。当鼠标移动到快速时,某些项目会被跳过,因此它们没有添加正确的颜色标签并保持不变。如果鼠标移动缓慢,则所有项目都会被正确选择。下面是新代码和问题的另一个图像。

更新 2:我已经解决了这个问题并发布了工作代码,只是为了完整性和将来帮助其他人。我最终使用了自己的选择系统,而不是内置的系统。

import tkinter as tk

import tkinter.ttk as ttk



class App(tk.Tk):

    def __init__(self):

        super().__init__()

        self.title('Treeview Demo')

        self.geometry('300x650')

        self.rowconfigure(0, weight=1)

        self.columnconfigure(0, weight=1)


        tv = self.tv = ttk.Treeview(self)

        tv.heading('#0', text='Name')

        # Populate tree with test data.

        for idx in range(0, 4):

            tv.insert('', idx, f'!{idx}', text=f'Item {idx+1}', tags='TkTextFont', open=1)

            iid = f'!{idx}_!{idx}'

            tv.insert(f'!{idx}', '0', iid, text=f'Python {idx+1}', tags='TkTextFont', open=1)

            for i in range(0, 5):

                tv.insert(iid, f'{i}', f'{iid}_!{i}', text=f'Sub item {i+1}', tags='TkTextFont')


        tv.grid(sticky='NSEW')

        self.active_item = None


        def motion(_):

            x, y = tv.winfo_pointerxy()

            item = tv.identify('item', x - tv.winfo_rootx(), y - tv.winfo_rooty())

            if not item or item == self.active_item:

                return


            if not self.active_item:

                self.active_item = item


            tv.selection_toggle(item)

            self.active_item = item


        def escape(_):

            tv.selection_remove(tv.selection())


        def button_press(_):

            self.bind('<Motion>', motion)


        def button_release(_):

            self.unbind('<Motion>')

            self.active_item = None


        self.bind('<Escape>', escape)

        self.bind('<Button-1>', button_press)

        self.bind('<ButtonRelease-1>', button_release)



def main():

    app = App()

    app.mainloop()



if __name__ == '__main__':

    main()


慕田峪9158850
浏览 273回答 1
1回答

HUWWW

工作示例:在 Windows 和 Linux 中测试和工作更新:我已经更新了代码,一切都在 Windows 和 Linux 中运行,尽管在 Windows 中,蓝色透明窗口在从右到左调整大小时会抖动,从左到右很好。在 Linux 中不会发生抖动,有人知道为什么吗?如果有人可以让我知道下面的代码是否适用于 MacOS,那也很棒。import tkinter as tkimport tkinter.ttk as ttkimport tkinter.font as tkfontclass Treeview(ttk.Treeview):&nbsp; &nbsp; def __init__(self, parent, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(parent, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; self.root = parent.winfo_toplevel()&nbsp; &nbsp; &nbsp; &nbsp; self.selected_items = []&nbsp; &nbsp; &nbsp; &nbsp; self.origin_x = \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.origin_y = \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.active_item = \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.origin_item = None&nbsp; &nbsp; &nbsp; &nbsp; sw = self.select_window = tk.Toplevel(self.root)&nbsp; &nbsp; &nbsp; &nbsp; sw.wait_visibility(self.root)&nbsp; &nbsp; &nbsp; &nbsp; sw.withdraw()&nbsp; &nbsp; &nbsp; &nbsp; sw.config(bg='#00aaff')&nbsp; &nbsp; &nbsp; &nbsp; sw.overrideredirect(True)&nbsp; &nbsp; &nbsp; &nbsp; sw.wm_attributes('-alpha', 0.3)&nbsp; &nbsp; &nbsp; &nbsp; sw.wm_attributes("-topmost", True)&nbsp; &nbsp; &nbsp; &nbsp; self.font = tkfont.nametofont('TkTextFont')&nbsp; &nbsp; &nbsp; &nbsp; self.style = parent.style&nbsp; &nbsp; &nbsp; &nbsp; self.linespace = self.font.metrics('linespace') + 5&nbsp; &nbsp; &nbsp; &nbsp; self.bind('<Escape>', self.tags_reset)&nbsp; &nbsp; &nbsp; &nbsp; self.bind('<Button-1>', self.button_press)&nbsp; &nbsp; &nbsp; &nbsp; self.bind('<ButtonRelease-1>', self.button_release)&nbsp; &nbsp; def fixed_map(self, option):&nbsp; &nbsp; &nbsp; &nbsp; return [elm for elm in self.style.map("Treeview", query_opt=option) if elm[:2] != ("!disabled", "!selected")]&nbsp; &nbsp; def tag_add(self, tags, item):&nbsp; &nbsp; &nbsp; &nbsp; self.tags_update('add', tags, item)&nbsp; &nbsp; def tag_remove(self, tags, item=None):&nbsp; &nbsp; &nbsp; &nbsp; self.tags_update('remove', tags, item)&nbsp; &nbsp; def tag_replace(self, old, new, item=None):&nbsp; &nbsp; &nbsp; &nbsp; for item in (item,) if item else self.tag_has(old):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tags_update('add', new, item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tags_update('remove', old, item)&nbsp; &nbsp; def tags_reset(self, _=None):&nbsp; &nbsp; &nbsp; &nbsp; self.tag_remove(('selected', '_selected'))&nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('selected_odd', 'odd')&nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('selected_even', 'even')&nbsp; &nbsp; def tags_update(self, opt, tags, item):&nbsp; &nbsp; &nbsp; &nbsp; def get_items(node):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.append(node)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for node in self.get_children(node):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get_items(node)&nbsp; &nbsp; &nbsp; &nbsp; if not tags:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; elif isinstance(tags, str):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags = (tags,)&nbsp; &nbsp; &nbsp; &nbsp; if not item:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for child in self.get_children():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get_items(child)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items = (item,)&nbsp; &nbsp; &nbsp; &nbsp; for item in items:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tags = list(self.item(item, 'tags'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for _tag in tags:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if opt == 'add':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if _tag not in _tags:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tags.append(_tag)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif opt == 'remove':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if _tag in _tags:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _tags.pop(_tags.index(_tag))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.item(item, tags=_tags)&nbsp; &nbsp; def button_press(self, event):&nbsp; &nbsp; &nbsp; &nbsp; self.origin_x, self.origin_y = event.x, event.y&nbsp; &nbsp; &nbsp; &nbsp; item = self.origin_item = self.active_item = self.identify('item', event.x, event.y)&nbsp; &nbsp; &nbsp; &nbsp; sw = self.select_window&nbsp; &nbsp; &nbsp; &nbsp; sw.geometry('0x0+0+0')&nbsp; &nbsp; &nbsp; &nbsp; sw.deiconify()&nbsp; &nbsp; &nbsp; &nbsp; self.bind('<Motion>', self.set_selected)&nbsp; &nbsp; &nbsp; &nbsp; if not item:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not event.state & 1 << 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tags_reset()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; if event.state & 1 << 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.tag_has('odd', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_add('selected', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('odd', 'selected_odd', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('even', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_add('selected', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('even', 'selected_even', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('selected_odd', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('selected_odd', 'odd', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('selected_even', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('selected_even', 'even', item)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tags_reset()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_add('selected', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.tag_has('odd', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('odd', 'selected_odd', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('even', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('even', 'selected_even', item)&nbsp; &nbsp; def button_release(self, _):&nbsp; &nbsp; &nbsp; &nbsp; self.select_window.withdraw()&nbsp; &nbsp; &nbsp; &nbsp; self.unbind('<Motion>')&nbsp; &nbsp; &nbsp; &nbsp; for item in self.selected_items:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.tag_has('odd', item) or self.tag_has('even', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_remove(('selected', '_selected'), item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('_selected', 'selected', item)&nbsp; &nbsp; def get_selected(self):&nbsp; &nbsp; &nbsp; &nbsp; return sorted(self.tag_has('selected_odd') + self.tag_has('selected_even'))&nbsp; &nbsp; def set_selected(self, event):&nbsp; &nbsp; &nbsp; &nbsp; def selected_items():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window_y = int(self.root.geometry().rsplit('+', 1)[-1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; titlebar_height = self.root.winfo_rooty() - window_y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sw = self.select_window&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start = sw.winfo_rooty() - titlebar_height - window_y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end = start + sw.winfo_height()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while start < end:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node = self.identify('item', event.x, start)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not node or node in items:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.append(node)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return sorted(items)&nbsp; &nbsp; &nbsp; &nbsp; def set_row_colors():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items = self.selected_items = selected_items()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for item in items:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.tag_has('selected', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item == self.origin_item:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.tag_has('selected_odd', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('selected_odd', 'odd', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('selected_even', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('selected_even', 'even', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('odd', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('odd', 'selected_odd', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('even', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('even', 'selected_even', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_add('_selected', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for item in self.tag_has('_selected'):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item not in items:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_remove('_selected', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.tag_has('odd', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('odd', 'selected_odd', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('even', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('even', 'selected_even', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('selected_odd', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('selected_odd', 'odd', item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.tag_has('selected_even', item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tag_replace('selected_even', 'even', item)&nbsp; &nbsp; &nbsp; &nbsp; root_x = self.root.winfo_rootx()&nbsp; &nbsp; &nbsp; &nbsp; if event.x < self.origin_x:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width = self.origin_x - event.x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coord_x = root_x + event.x&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width = event.x - self.origin_x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coord_x = root_x + self.origin_x&nbsp; &nbsp; &nbsp; &nbsp; if coord_x+width > root_x+self.winfo_width():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width -= (coord_x+width)-(root_x+self.winfo_width())&nbsp; &nbsp; &nbsp; &nbsp; elif self.winfo_pointerx() < root_x:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width -= (root_x - self.winfo_pointerx())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coord_x = root_x&nbsp; &nbsp; &nbsp; &nbsp; root_y = self.winfo_rooty()&nbsp; &nbsp; &nbsp; &nbsp; if event.y < self.origin_y:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height = self.origin_y - event.y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coord_y = root_y + event.y&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height = event.y - self.origin_y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coord_y = root_y + self.origin_y&nbsp; &nbsp; &nbsp; &nbsp; if coord_y+height > root_y+self.winfo_height():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height -= (coord_y+height)-(root_y+self.winfo_height())&nbsp; &nbsp; &nbsp; &nbsp; elif self.winfo_pointery() < root_y + self.linespace:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height -= (root_y - self.winfo_pointery() + self.linespace)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coord_y = root_y + self.linespace&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if height < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height = self.winfo_rooty() + self.origin_y&nbsp; &nbsp; &nbsp; &nbsp; set_row_colors()&nbsp; &nbsp; &nbsp; &nbsp; self.select_window.geometry(f'{width}x{height}+{coord_x}+{coord_y}')class App(tk.Tk):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; def print_selected_items(_):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(tv.get_selected())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'break'&nbsp; &nbsp; &nbsp; &nbsp; style = self.style = ttk.Style()&nbsp; &nbsp; &nbsp; &nbsp; tv = Treeview(self)&nbsp; &nbsp; &nbsp; &nbsp; style.map("Treeview", foreground=tv.fixed_map("foreground"), background=tv.fixed_map("background"))&nbsp; &nbsp; &nbsp; &nbsp; tv.heading('#0', text='Name')&nbsp; &nbsp; &nbsp; &nbsp; tv.tag_configure('odd', background='#ffffff')&nbsp; &nbsp; &nbsp; &nbsp; tv.tag_configure('even', background='#aaaaaa')&nbsp; &nbsp; &nbsp; &nbsp; tv.tag_configure('selected_odd', background='#b0eab2')&nbsp; &nbsp; &nbsp; &nbsp; tv.tag_configure('selected_even', background='#25a625')&nbsp; &nbsp; &nbsp; &nbsp; color_tag = 'odd'&nbsp; &nbsp; &nbsp; &nbsp; for idx in range(0, 4):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Populating the tree with test data.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color_tag = 'even' if color_tag == 'odd' else 'odd'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.insert('', idx, f'{idx}', text=f'Item {idx+1}', open=1, tags=(color_tag,))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color_tag = 'even' if color_tag == 'odd' else 'odd'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iid = f'{idx}_{0}'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.insert(f'{idx}', '0', iid, text=f'Menu {idx+1}', open=1, tags=(color_tag,))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i in range(0, 5):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color_tag = 'even' if color_tag == 'odd' else 'odd'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.insert(iid, i, f'{iid}_{i}', text=f'Sub item {i+1}', tags=(color_tag,))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color_tag = 'even' if color_tag == 'odd' else 'odd'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.insert(iid, 5, f'{iid}_{5}', text=f'Another Menu {idx+1}', open=1, tags=(color_tag,))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iid = f'{iid}_{5}'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i in range(0, 3):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color_tag = 'even' if color_tag == 'odd' else 'odd'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.insert(iid, i, f'{iid}_{i}', text=f'Sub item {i+1}', tags=(color_tag,))&nbsp; &nbsp; &nbsp; &nbsp; self.title('Treeview Demo')&nbsp; &nbsp; &nbsp; &nbsp; self.geometry('275x650+3000+250')&nbsp; &nbsp; &nbsp; &nbsp; self.rowconfigure(0, weight=1)&nbsp; &nbsp; &nbsp; &nbsp; self.columnconfigure(0, weight=1)&nbsp; &nbsp; &nbsp; &nbsp; tv.config(selectmode="none")&nbsp; &nbsp; &nbsp; &nbsp; tv.grid(sticky='NSEW')&nbsp; &nbsp; &nbsp; &nbsp; button = ttk.Button(self, text='Get Selected Items')&nbsp; &nbsp; &nbsp; &nbsp; button.grid()&nbsp; &nbsp; &nbsp; &nbsp; button.bind('<Button-1>', print_selected_items)def main():&nbsp; &nbsp; app = App()&nbsp; &nbsp; app.mainloop()if __name__ == '__main__':&nbsp; &nbsp; main()
随时随地看视频慕课网APP

相关分类

Python
我要回答