Python Tkinter:传递列表参数以填充另一个列表

Python Tkinter: Passing List Arguments to Populate another List

总结 我正在 Python 2.7 中构建 Tkinter 程序。目标是使用可用的可移动闪存驱动器自动填充列表。当用户从列表中选择一个或多个驱动器时,所选驱动器(sel_drv)将传递给另一个函数,该函数将在驱动器中搜索特定文件类型并生成一个列表以填充在屏幕上.

问题 我 运行 遇到的问题是函数的顺序。当用户选择一个驱动器时,它传递了 drv_sel 变量,然后附加到 sel_drives 列表。我一直在尝试在其定义之前使用 sel_drv 或 TypeErrors 时出错。

重要模块: drive_ctypes.find_rmdrv() - 此函数将在计算机中搜索可移动驱动器并填充列表。 (这工作正常)

file_search.file_search(sel_drives) - 此函数将搜索驱动器(sel_drives) 和return 某种类型的所有文件的列表。 (此功能有效,但我无法正确传递 sel_drive。

背景 我是编程和 Python 的新手,包括 class 和 OOP。我将不胜感激任何可能帮助我了解更多关于 Tkinter 和 Classes/OOP.

的资源

这里是错误

Traceback (most recent call last):
  File "C:\Python27\window.py", line 70, in <module>
    main()
  File "C:\Python27\window.py", line 64, in main
    ex = Example(root)
  File "C:\Python27\window.py", line 13, in __init__
    self.initUI()
  File "C:\Python27\window.py", line 33, in initUI
    for i in sel_files:
TypeError: 'instancemethod' object is not iterable

代码如下:

from Tkinter import *
import drive_ctypes
import file_search

#global sel_files
#sel_files[]

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent        
        self.initUI()

    def initUI(self):      
        self.parent.title("Listbox") 
        self.pack(fill=BOTH, expand=1)

    ## Drive Select List Box
        global rdrive
        rdrive = drive_ctypes.find_rmdrv()
        lb = Listbox(self, height=10, selectmode=MULTIPLE)
        for i in rdrive:
            lb.insert(END, i)

        lb.bind("<<ListboxSelect>>", self.onSelect)
        sel_files = self.onSelect

        lb.grid(row =3, column =2)

    ## File Select List Box
        flb = Listbox(self, height=10, selectmode=MULTIPLE)    
        for i in sel_files:
            flb.insert(END, i)            
        flb.grid(row =3, column =4) 


    def onSelect(self, val):
        sender = val.widget
        drv_sel = sender.curselection()
        print drv_sel
        ## List of Drives Selected
        sel_drives = []
        for i in drv_sel:
            drive_with_gb = rdrive[i]
            ## This trims the name of the drive for the file_search function
            drive = drive_with_gb[:-9]
            ## This is creating a list to pass to the file_search function
            sel_drives.append(drive)
        sel_files = file_search.file_search(sel_drives)
        return sel_files



    def findfiles(self,val):
        sender = val.widget




def main():

非常感谢任何帮助!

我修改了您的示例,并制作了虚拟 rdrive 和 sel_files 变量。否则我无法重现你的问题。看看这个:

from Tkinter import *



# dummy list so that the code does not relay on actually drives and files
rdrive = ['drive1','drive2','drive3']

sel_files = {'drive1': ['file1','file2'],
                  'drive2': ['file3','file4'],
                  'drive3': ['file6','file5']}

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Listbox")
        self.pack(fill=BOTH, expand=1)

        # Drive Select List Box
        # global rdrive
        # rdrive = drive_ctypes.find_rmdrv()            

        # use dummy rdrive instead of physical drives. Otherwise,
        # cant reproduce the problem.  

        self.lb = Listbox(self, height=10, selectmode=MULTIPLE)
        for i in rdrive:
            self.lb.insert(END, i)

        self.lb.bind("<<ListboxSelect>>", self.onSelect)



        self.lb.grid(row =3, column =2)

        ## File Select List Box
        self.flb = Listbox(self, height=10, selectmode=MULTIPLE)

        self.flb.grid(row =3, column =4)


    def onSelect(self, event):
        # most changes are here. GUI programming is event driven, so you need
        # to get the list of files for selected drive (i.e. when selection even occurs).
        # Also here you respond the the even, so that the right list is populated.


        # get widget (i.e. right listbox) and currently selected item(s) 
        widget = event.widget
        selection=widget.curselection()

        files_avalibe = []

        # if something was selected, than get drives for which it was selected
        # and retrieve files for each drive
        if selection:


            for drive_i in selection:
                selected_drive = rdrive[drive_i]
                files_avalibe += sel_files[selected_drive]


            print(files_avalibe)

        # once we have files from the selected drive, list them 
        # in the right list box 
        self.update_file_list(files_avalibe)



    def update_file_list(self, file_list):
          # updates right listbox
          self.flb.delete(0, END)
          for i in file_list:
            self.flb.insert(END, i)




    def findfiles(self,val):
        sender = val.widget




root = Tk()
f = Example(root)
root.mainloop()

此处显示其工作原理:

很抱歉没有详细解释更改的内容。但我认为你可以自己解决这些问题。您的代码的主要问题是 sel_files = self.onSelect。这只是将函数分配给 sel_files,而不是输出。要获取所选驱动器的实际文件列表,需要在 onSelect 中完成。