2016-12-05 80 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