我想加载一组图像,将它们分成两组,然后在窗口中并排显示这些图像。我还要添加一个按钮来选择要显示的对
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])
photo1和photo2行中的p.S.都是路径存储在photo1[0]中,图像大小存储在photo1[1]中的列表
问题是
tkinter
回调¹不直接支持参数和²忽略返回值。这个问题是可以解决的¹使用带有默认参数和²使用可变对象(例如列表)作为默认参数,因为当回调函数修改它时,更改会反映在调用者作用域中例如,你可以用一个参数来定义
select_files
,一个列表,这是一个你可以随意修改的可变参数然后,在main中修改
command=...
以引入默认参数因此,最终,您可以访问每对图像文件名
在实践中证明
还有一个反例
这表明您应该从不赋值给函数参数
相关问题 更多 >
编程相关推荐