第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用tkinter Treeview小部件顯示目錄內(nèi)容

使用tkinter Treeview小部件顯示目錄內(nèi)容

手掌心 2021-03-10 14:58:58
目前,我正在開發(fā)一個(gè)程序,該程序具有自己的項(xiàng)目文件,并且內(nèi)部有子文件之類的東西,我想知道如何使用treeview小部件顯示項(xiàng)目文件中的所有子文件,有什么想法嗎?
查看完整描述

2 回答

?
蠱毒傳說

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊


CPython的源代碼中有一個(gè)示例,說明如何用目錄的內(nèi)容遞歸地填充Treeview,這基本上是它的工作方式(為了更好的可讀性,我刪除了事件綁定并將其包裝在一個(gè)類中):


import os

import tkinter as tk

import tkinter.ttk as ttk


class App(tk.Frame):

    def __init__(self, master, path):

        tk.Frame.__init__(self, master)

        self.tree = ttk.Treeview(self)

        ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)

        xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)

        self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)

        self.tree.heading('#0', text=path, anchor='w')


        abspath = os.path.abspath(path)

        root_node = self.tree.insert('', 'end', text=abspath, open=True)

        self.process_directory(root_node, abspath)


        self.tree.grid(row=0, column=0)

        ysb.grid(row=0, column=1, sticky='ns')

        xsb.grid(row=1, column=0, sticky='ew')

        self.grid()


    def process_directory(self, parent, path):

        for p in os.listdir(path):

            abspath = os.path.join(path, p)

            isdir = os.path.isdir(abspath)

            oid = self.tree.insert(parent, 'end', text=p, open=False)

            if isdir:

                self.process_directory(oid, abspath)


root = tk.Tk()

path_to_my_project = # ...

app = App(root, path=path_to_my_project)

app.mainloop()

更新:正如@ArtOfWarfare所提到的,可以使用該<<TreeviewOpen>>事件懶散地填充樹。為了模擬封閉的節(jié)點(diǎn),我使用了一個(gè)空的子項(xiàng),該子項(xiàng)在打開目錄時(shí)將被刪除:


import os

import tkinter as tk

import tkinter.ttk as ttk



class App(object):

    def __init__(self, master, path):

        self.nodes = dict()

        frame = tk.Frame(master)

        self.tree = ttk.Treeview(frame)

        ysb = ttk.Scrollbar(frame, orient='vertical', command=self.tree.yview)

        xsb = ttk.Scrollbar(frame, orient='horizontal', command=self.tree.xview)

        self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)

        self.tree.heading('#0', text='Project tree', anchor='w')


        self.tree.grid()

        ysb.grid(row=0, column=1, sticky='ns')

        xsb.grid(row=1, column=0, sticky='ew')

        frame.grid()


        abspath = os.path.abspath(path)

        self.insert_node('', abspath, abspath)

        self.tree.bind('<<TreeviewOpen>>', self.open_node)


    def insert_node(self, parent, text, abspath):

        node = self.tree.insert(parent, 'end', text=text, open=False)

        if os.path.isdir(abspath):

            self.nodes[node] = abspath

            self.tree.insert(node, 'end')


    def open_node(self, event):

        node = self.tree.focus()

        abspath = self.nodes.pop(node, None)

        if abspath:

            self.tree.delete(self.tree.get_children(node))

            for p in os.listdir(abspath):

                self.insert_node(node, p, os.path.join(abspath, p))



if __name__ == '__main__':

    root = tk.Tk()

    app = App(root, path='.')

    root.mainloop()


查看完整回答
反對(duì) 回復(fù) 2021-03-26
?
守候你守候我

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊

可以通過以下方式在支持Python 3.4及更高版本的第二行和第三行更改導(dǎo)入:


import tkinter as tk

import tkinter.ttk as ttk

tkinter中的小寫字母t代替了Tkinter中的大寫字母T,因?yàn)镻ython 3.4及更高版本不再識(shí)別Tkinter;消除了“無法識(shí)別的參考”錯(cuò)誤。


在較新的Python發(fā)行版中,完全合格的導(dǎo)入指令對(duì)所有點(diǎn)符號(hào)都更加嚴(yán)格,因此tkinter.ttk是ttk所必需的,從而消除了對(duì)重復(fù)的完全合格引用的需求。告誡:有人認(rèn)為導(dǎo)入tk.ttk就足夠了,但是以某種方式引起了參考錯(cuò)誤;消除過多的指針和有條件的宏處理使我選擇了上面的格式-還有其他可能,但這是最容易使用的形式。


path_to_my_project =#...會(huì)引發(fā)錯(cuò)誤,但僅是占位符(盡管仍然可以使用),可以更改為以下內(nèi)容:


path_to_my_project = "" # ...

請(qǐng)記住,如果在Windows中運(yùn)行腳本,則使用反斜杠的文字文件路徑會(huì)引發(fā)錯(cuò)誤。您必須使用前面的反斜杠(雙反斜杠)對(duì)文件路徑url中的反斜杠進(jìn)行轉(zhuǎn)義,如下所示:


path_to_my_project = "C:\\Users\\userName\\Desktop\\projDir" #Windows file paths

在文件路徑中使用反斜杠轉(zhuǎn)義反斜杠可以消除“ Unicode轉(zhuǎn)義字符”錯(cuò)誤,其中“ C:\ Users”解析為在“用戶”中轉(zhuǎn)義控制字符U,而這并不是我們最初想要的。將路徑轉(zhuǎn)換為字符串將無法工作,因?yàn)闀?huì)引發(fā)相同的錯(cuò)誤,因此:


path_to_my_project = str("C:\Users\userName\Desktop\projDir")

...無效。


上面的腳本示例針對(duì)在Windows 10 64bit上運(yùn)行的Python 3.4 64bit進(jìn)行了這些修改,沒有任何問題,并且非常干凈,可靠。


我喜歡它-輕松運(yùn)行(簡(jiǎn)單的更新),沒有錯(cuò)誤,警告或無法解釋的解決方法,bs和hacks。豎起大拇指。


查看完整回答
反對(duì) 回復(fù) 2021-03-26
  • 2 回答
  • 0 關(guān)注
  • 471 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)