跟踪列表框小部件信息

时间:2013-12-05 02:38:11

标签: python user-interface python-3.x tkinter

我正在尝试使用tkinter创建一个GUI来选择文本文件,最终结果是文件名以及用于其他程序的路径。文本文件可能不在同一文件夹中 GUI目前能够:

  1. 在文件夹之间移动
  2. 在单独的框中显示当前文件夹中的文件夹和所有文本文件
  3. 选择文件并在单独的框中显示所选文件名
  4. 我为路径和文件名携带一个单独的StringVar,它与显示所选文件名的列表框小部件的索引匹配,或至少尝试。我没有找到一种方法来简单地添加或删除StringVar中的'line',就像列表或数组一样。所以我尝试“提取”文件(通过将字符串转换回列表),以便使用.set将它们重写为相同的StringVar,但这并不是很好。主要问题是每次重写列表时我都会加倍'\'。

    如果有人知道更容易/更好的实现方式,我希望,我在描述我会非常感谢输入或指导其他资源。如果您想使用它,则会附加代码。我也是python和tkinter的新手,所以如果你看到任何可以做得更好的事情,我也想听到。感谢您的任何意见。

    from tkinter import *
    from tkinter import ttk
    import data_Modules as dM
    import os
    
    ################################################################################
    # Change to a nested folder
    ################################################################################
    def update(*arg):
        print(entry1.get())
        folders.set(value =
                    tuple([element for element
                           in os.listdir(entry1.get())
                           if os.path.isdir(os.path.join(entry1.get(), element))]))
        dirFiles = [os.path.join(entry1.get(), element) for element
                    in os.listdir(entry1.get())
                    if element.split('.')[-1] == 'txt']
        dirFiles.sort()
        listFiles.set(value =
                      tuple([line.split('\\')[-1] for line in dirFiles]))
    
    ################################################################################
    # Return to prior folder
    ################################################################################    
    def uplevel(*arg):
        priorPath = hardDrive
        for line in entry1.get().split('\\')[1:-1]:
            priorPath = os.path.join(priorPath, line)
        parentDir.set(priorPath)
        update()
    
    ################################################################################
    # Add file to 'memory'
    ################################################################################
    def select(*arg):
        dirFiles = [os.path.join(entry1.get(), element) for element
                    in os.listdir(entry1.get())
                    if element.split('.')[-1] == 'txt']
        dirFiles.sort()
        print('dirFiles')
        print(dirFiles[int(boxlist2.curselection()[0])])
        print(dirFiles[0])
        temp = retVar(filePath)
        print('temp In')
        for line in temp:
            print(line)
        if all([element != dirFiles[int(boxlist2.curselection()[0])]
               for element in temp]):
            temp.append(dirFiles[int(boxlist2.curselection()[0])])
            temp.sort()
            print('temp Out')
            for line in temp:
                print(line)
            filePath.set(value = tuple(temp))    
        selFiles.set(value = tuple([line.split('\\')[-1] for line in temp]))
    
    ################################################################################
    # Remove file from 'memory'
    ################################################################################
    def deselect(*arg):
        temp = retVar(filePath)
        temp.sort()
        del temp[int(boxlist3.curselection()[0])]
        filePath.set(value = tuple(temp))
        selFiles.set(value = tuple([line.split('\\')[-1] for line in temp]))
    
    ################################################################################
    # Attempt at retrieving data
    ################################################################################
    def retVar(strVar):
        if strVar.get() == '':
            print('\n')
            return []
        else:
            if strVar.get()[-2:] == ',)':
                print([strVar.get()[2:-3]])
                return [strVar.get()[2:-3]]
            else:
                print(strVar.get()[3:-3].split('\', \''))
                return strVar.get()[3:-3].split('\', \'')
    
    # Create window and Change name of window
    root = Tk()
    root.title('Main Window')
    # Main hard drive for the computer
    hardDrive = 'C:\\'
    # Create necessary variables
    parentDir = StringVar()
    folders = StringVar()
    listFiles = StringVar()
    selFiles = StringVar()
    filePath = StringVar()
    parentDir.set(value = hardDrive)
    dirFiles = []
    
    # Generate all the widgets
    entry1 = ttk.Entry(root, textvariable = parentDir)
    entry1.grid(column = 0, row = 1, sticky = 'WE')
    entry1.columnconfigure(0, weight = 1)
    entry1.rowconfigure(0, weight = 1)
    
    label1 = ttk.Label(root, text = 'Current Directory')
    label1.grid(column = 0, row = 0, sticky = 'WE')
    label1.columnconfigure(0, weight = 1)
    label1.rowconfigure(0, weight = 1)
    
    #Not currently used
    #label2 = ttk.Label(root, text = 'Parent')
    #label2.grid(column = 2, row = 1, sticky = 'WE')
    #label2.columnconfigure(0, weight = 1)
    #label2.rowconfigure(0, weight = 1)
    
    label3 = ttk.Label(root, textvariable = parentDir.get())
    label3.grid(column = 3, row = 2, columnspan = 2, sticky = 'WE')
    label3.columnconfigure(0, weight = 1)
    label3.rowconfigure(0, weight = 1)
    
    label4 = ttk.Label(root, text = 'Folders')
    label4.grid(column = 0, row = 2, sticky = 'WE')
    label4.columnconfigure(0, weight = 1)
    label4.rowconfigure(0, weight = 1)
    
    label5 = ttk.Label(root, text = 'Files')
    label5.grid(column = 3, row = 2, sticky = 'WE')
    label5.columnconfigure(0, weight = 1)
    label5.rowconfigure(0, weight = 1)
    
    label6 = ttk.Label(root, text = 'Files')
    label6.grid(column = 5, row = 2, sticky = 'WE')
    label6.columnconfigure(0, weight = 1)
    label6.rowconfigure(0, weight = 1)
    
    button1 = ttk.Button(root, text = 'Go', command = update)
    button1.grid(column = 1, row = 1, sticky = 'W')
    button1.columnconfigure(0, weight = 1)
    button1. rowconfigure(0, weight = 1)
    
    button2 = ttk.Button(root, text = 'Back', command = uplevel)
    button2.grid(column = 2, row = 1)
    button2.columnconfigure(0, weight = 1)
    button2. rowconfigure(0, weight = 1)
    
    button3 = ttk.Button(root, text = 'deselect', command = deselect)
    button3.grid(column = 5, row = 6)
    button3.columnconfigure(0, weight = 1)
    button3. rowconfigure(0, weight = 1)
    
    button4 = ttk.Button(root, text = 'select', command = select)
    button4.grid(column = 5, row = 4)
    button4.columnconfigure(0, weight = 1)
    button4. rowconfigure(0, weight = 1)
    
    update()
    
    boxlist1 = Listbox(root, listvariable = folders, height = 10,
                       font = 18, width = 40, selectmode = 'single')
    boxlist1.grid(column = 0, row = 3, columnspan = 3, rowspan = 5)
    boxlist1.columnconfigure(0, weight = 1)
    boxlist1.rowconfigure(0, weight = 1)
    
    boxlist2 = Listbox(root, listvariable = listFiles, height = 10,
                       font = 18, width = 40, selectmode = 'single')
    boxlist2.grid(column = 3, row = 3, columnspan = 2, rowspan = 5)
    boxlist2.columnconfigure(0, weight = 1)
    boxlist2.rowconfigure(0, weight = 1)
    
    
    boxlist3 = Listbox(root, listvariable = selFiles, height = 10,
                       font = 18, width = 40, selectmode = 'single')
    boxlist3.grid(column = 6, row = 3, columnspan = 2, rowspan = 5)
    boxlist3.columnconfigure(0, weight = 1)
    boxlist3.rowconfigure(0, weight = 1)
    
    
    
    boxlist1.bind('<Double-Button-1>', lambda e: print(boxlist1.get(int(boxlist1.curselection()[0]))))
    boxlist1.bind('<Double-Button-1>', lambda e: parentDir.set(os.path.join(entry1.get(), boxlist1.get(int(boxlist1.curselection()[0])))))
    boxlist1.bind('<Double-ButtonRelease-1>', update)
    
    
    root.mainloop()
    

1 个答案:

答案 0 :(得分:0)

我会将我的值保存在iterable / sequence中,并通过调用以下函数绘制listbow小部件:

def redraw_listbox(w, seq):
    w.delete(0, "end")
    for i, elem in enumerate(seq):
        w.insert(i, str(elem))