python 错误 "unknown option "1": 必须移动或滚动" tkinter canvas

python error "unknown option "1": must be moveto or scroll" tkinter canvas

我正在尝试使用 tkinter 创建一个图像查看程序,一切正常,除了:我想要一个用户选择的目录中所有图像的列表,这个列表应该显示在 canvas, 附加了一个水平滚动条,我看到这个页面上的其他人也遇到了同样的问题,有人说使用框架,这是有效的,但是当我想使用滚动条时,我得到以下错误:

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in/  
        __call__
        return self.func(*args)
      File "/usr/lib/python3.4/tkinter/__init__.py", line 1549, in xview
        res = self.tk.call(self._w, 'xview', *args)
    _tkinter.TclError: unknown option "1": must be moveto or scroll
    >>> 

我无法从中得出结论,所以请大家帮忙,这里是实际的代码:

    from tkinter import *
    from PIL import Image,ImageTk
    import os
    import time

    #next image
    def Next():
        i += 1
        global img,i
        print(i)

        display_images(data[i])
        

    #previous image
    def Previous():
        i -= 1
        global img,i
        print(i)

        display_images(data[i])
    #list of images on canvas
    def show_images():
        global photoButtons,imgFile,imOpen,imgFoto,resized,photolist
        j = 0
        for number in data:
            print(">>>",number)
            imOpen.append(Image.open(data[j]))
            imgFile.append(imOpen[j])
            resized.append(imgFile[j].resize((50,50),Image.ANTIALIAS))
            imgFoto.append(ImageTk.PhotoImage(resized[j]))
            photoButtons.append(Button(photolist, text=j,/
            image=imgFoto[j],command=display_images(data[j]),width=50,/
            height = 50))
            photoButtons[j].pack(side=RIGHT)
            j += 1    

    #display selected image
    def display_images(image_name):
        img = Image.open(image_name)
        size = img.resize((700,500),Image.ANTIALIAS)
        photoviewer.image = ImageTk.PhotoImage(size)
        photoviewer.create_image(0,0, image=photoviewer.image,anchor='nw')

    # END DEF's

    global i

    i=0
    #root
    root = Tk()

    #root size
    root.geometry("1000x720+0+0")
    #canvas for displaying image
    photoviewer = Canvas(root, width=700, height=500)
    photoviewer.grid(row = 0, column = 0)
    photoviewer.place(x=295, y=215,)
    #frame (ive got this from another page of stack overflow)
    frame=Frame(root,width=300,height=300)
    frame.grid(row=0,column=0)
    #canvas for displaying list of images
    photolist = Canvas(frame, width=395, height=50)
    #scrollbar
    scrl=Scrollbar(frame,orient=HORIZONTAL)
    scrl.pack(side=BOTTOM,fill=X)
    scrl.config(command=photolist.xview)
    photolist.pack(side=TOP)

    imgFile = []
    imOpen=[]
    imgFoto=[]
    resized = []
    #get the directory with the images from the user
    data = os.listdir()
    print(data)
    cd = input("change directory to:   ")

    while cd != "x":
        
       
        os.chdir(cd)
        data = os.listdir()
        print(data)
        cd = input("change directory to:   ")

    #end

    #creating button for next image
    nxt=Button(root,text=">",command= Next)
    #creating button for previous image
    prvs=Button(root,text="<",command= Previous)
    photoButtons = []
    show_images()

    root.mainloop()

这个想法是,用户应该选择一个目录,然后在他们按下 'x' 之后,程序应该调用 show_images,它应该在 canvas 上显示该目录中的所有图像附上滚动条后,用户应该可以在它们之间进行选择,还可以选择下一个和上一个,但是,滚动条不起作用。

要将滚动条连接到小部件,您必须做两件事:告诉小部件要与哪个滚动条交互(通过 xscrollcommand and/or yscrollcommand),并告诉滚动条哪个要滚动的小部件(使用 command 属性。您忽略了第一部分。

在创建滚动条后的某处添加以下内容:

photolist.configure(xscrollcommand=scrl.set)

注意:如果您只想滚动一些图像,则无需费心在 canvas 中嵌入框架。您可以直接在 canvas 上创建图像。框架给你的好处是你可以使用 pack 所以你不必计算放置图像的坐标,但它增加了很多复杂性。由于您将图像并排放置,因此很容易计算每张图像的 x/y 坐标。