TkInter-使用滚动条在网格中显示多个图像

时间:2018-11-20 20:41:55

标签: python tkinter tkinter-canvas

我是Tkinter的新手,正在尝试在Window中显示多个图像。图像应显示在网格中,并在窗口的最右侧具有滚动条。下面的代码将滚动条显示在错误的位置。如何将滚动条放在窗口的最右侧。请参见下面的代码。

def display_images(folder, root): 
  filenames = os.listdir(folder)
  columns = 10
  image_count = 0
  window = Toplevel(root)
  window.wm_geometry("1200x800")
  canvas = Canvas(window, width = 1200, height = 800)
  canvas.grid(row=0, column=0, sticky= "news")
  #canvas.place(x=0, y=0)

  vsb = Scrollbar(window, orient="vertical", command=canvas.yview)
  vsb.grid(row=0, column=0, sticky="ns")
  canvas.configure(yscrollcommand= vsb.set)

  frame_image = Frame(canvas)
  frame_image.pack(expand=True, fill="both")
  #frame_image.grid_rowconfigure(0, weight = 1)
  #frame_image.grid_columnconfigure(0, weight = 1)
  canvas.create_window((0,0), window=frame_image, anchor="nw")

  for name in filenames:
    image_count += 1
    r, c = divmod(image_count - 1, columns)
    im = Image.open(os.path.join(folder, name))
    resized = im.resize((100,100), Image.ANTIALIAS)
    tkimage = ImageTk.PhotoImage(resized)
    myvar = Label(frame_image, image = tkimage)
    myvar.image = tkimage
    myvar.grid(row=r, column = c)
    #print "here"
  window.mainloop()

谢谢!

0 个答案:

没有答案