2016-12-05 37 views
0

我正在試圖製作一個程序,它能夠比較來自一個圖形中選定文件的數據。它們的排列順序以及它們之間的距離很重要。但是,我在使用Tkinter創建的窗口中選擇它們的順序與它們傳遞到程序的順序不同。爲了得到正確的順序,我必須選擇它們的順序是2,3,4,5,1,這導致程序中的順序爲1,2,3,4,5。在Tkinter中的文件選擇順序不是在Python程序中的順序

一旦你知道它,這不是一個大問題,但其目的是其他人也將使用該程序,所以我需要它儘可能簡單和健壯的工作。以下是我在Tkinter涉及的代碼片段。

root = Tkinter.Tk() 
filez = tkFileDialog.askopenfilenames(parent=root,title='Choose a file') 
filez= root.tk.splitlist(filez) 
root.destroy() 
+1

你將不得不建立自己的'tkFileDialog'來記住順序。或者創建對話框,讓你改變列表上的元素順序(從'tkFileDialog'獲得所有文件後)。 – furas

回答

1

控制文檔中列出的訂單沒有選項。最好的辦法是一個接一個地使用多個文件選擇對話框。

1

tkFileDialog不具備的功能remeber所選文件的順序,這樣你可以建立自己的FileDialog或...

...建立一些對話框,選擇文件的順序從tkFileDialog

拿到文件後,
import Tkinter as tk 
import tkFileDialog 


def Selector(data): 

    def append(widget, element, results, display): 
     # append element to list 
     results.append(element) 

     # disable button 
     widget['state'] = 'disabled' 

     # add element to label 
     current = display['text'] 
     if current: 
      current += '\n' 
     display['text'] = current + element 


    # create window 
    root = tk.Tk() 

    # list for correct order 
    results = [] 

    # label to display order 
    tk.Label(root, text='ORDER').pack() 
    l = tk.Label(root, anchor='w', justify='left') 
    l.pack(fill='x') 

    # buttons to select elements 
    tk.Label(root, text='SELECT').pack() 

    for d in data: 
     b = tk.Button(root, text=d, anchor='w') 
     b['command'] = lambda w=b, e=d, r=results, d=l:append(w, e, r, d) 
     b.pack(fill='x') 

    # button to close window 
    b = tk.Button(root, text='Close', command=root.destroy) 
    b.pack(fill='x', pady=(15,0)) 

    # start mainloop 
    root.mainloop() 

    return results 

# --- main --- 

root = tk.Tk() 
filez = tkFileDialog.askopenfilenames(parent=root,title='Choose a file') 
root.destroy() 
print(filez) 

filez = Selector(filez) 
print(filez) 
+0

很好,謝謝! – TomH

+0

我一直對我的評論速度非常快......雖然代碼的結果顯得很棒,但結果列表是空的。這可能是因爲程序在選擇窗口打開時繼續運行,因此在定義訂單之前它會出現錯誤。例如'mailoop()'(iniside'Selector')中的 – TomH

+0

不允許程序繼續運行。它必須等到您關閉選擇窗口。如果您的代碼中有其他窗口,則需要修改。 – furas