Skip to main content
 首页 » 编程设计

python 错误 "unknown option "1": must be moveto or scroll"tkinter Canvas

2025年04月02日15yjmyzz

我正在尝试使用 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 和/或 yscrollcommand),并告诉小部件滚动条要滚动的小部件(使用 command 属性。您忽略了第一部分。

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

photolist.configure(xscrollcommand=scrl.set) 

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