Python 使用路径数组显示图像对

Python 使用路径数组显示图像对,python,tkinter,Python,Tkinter,我想加载一组图像,将它们成对地分割,然后并排(成对地)在窗口中显示这些图像。我还将添加一个按钮来选择显示哪对 def select_files(): files = filedialog.askopenfilenames(title="Select photo", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*"))) # many lines of code for the algori

我想加载一组图像,将它们成对地分割,然后并排(成对地)在窗口中显示这些图像。我还将添加一个按钮来选择显示哪对

    def select_files():
        files = filedialog.askopenfilenames(title="Select photo", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))   
        # many lines of code for the algorithm that splits images into pair
        pairs.append([photo1, photo2])      


    root = Tk()

    selectButton = Button(root, text="Select", command=select_files)
    selectButton.place(x=5, y=500)


    show_first = ImageTk.PhotoImage(img1)
    show_second = ImageTk.PhotoImage(img2)

    panel1 = Label(root, image=show_first)
    panel1.place(x=5, y=5)

    panel2 = Label(root, image=show_second)
    panel2.place(x=200, y=5)


    root.geometry("%dx%d+500+500" % (550, 550))
    root.mainloop()
但是我如何将图像传递到先显示\然后显示\呢


pairs.append([photo1,photo2])
photo1photo2中的p.S.都是列表,路径存储在photo1[0]中,图像大小存储在photo1[1]

问题是
tkinter
回调“”不直接支持参数,并且忽略返回值。该问题可以通过使用带默认参数的
lambda
和作为所述默认参数的可变对象(例如列表)来解决,因为当回调函数修改它时,更改会反映在调用者范围中

例如,您可以使用一个参数、一个列表来定义
select_files
,这是一个您可以随意修改的可变参数

然后,在main中修改
命令=…
以引入默认参数

pairs = []
...
selectButton = Button(root, text = "Select",
                      command = lambda pairs=pairs: select_files(pairs))
因此,最终,您可以访问每对图像文件名

for fn1, fn2 in pairs:
    ...
实践证明,

>>> def gp(pairs):
...     pairs.append([1,2])
... 
>>> pairs = []
>>> (lambda p=pairs: gp(p))() 
>>> pairs
[[1, 2]]
>>> 
还有一个反例

>>> def gp(pairs):
...     pairs = [[1, 2]]
... 
>>> pairs = []
>>> (lambda p=pairs: gp(p))() 
>>> pairs
[]
>>> 

这表明您永远不应该为函数参数赋值…

“问题是tkinter回调不支持参数”-事实并非如此。你可以使用
lambda
functools调用需要参数的函数。partial
@BryanOakley我编辑了我的答案,引入了限制副词“directly”,你怎么看?我认为“滥用可变参数”这个短语有点误导和不清楚。
>>> def gp(pairs):
...     pairs = [[1, 2]]
... 
>>> pairs = []
>>> (lambda p=pairs: gp(p))() 
>>> pairs
[]
>>>