View the image, Canvas on Canvas with scroll bar. Throws this error: item configure () is missing 1 required positional argument: 'tagOrId'

View the image, Canvas on Canvas with scroll bar. Throws this error: item configure () is missing 1 required positional argument: 'tagOrId'

谁能帮帮我,我在 canvas 上放置了一个带有垂直和水平滚动条的图像,图像已经出现了,但是它产生了我放在最后的错误,我没有看到我哪里错了,我没有很好地处理 canvas。我更新了我的代码

def visualizar_imagen_consulta(self):
    try:
        self.tree3.item(self.tree3.selection())['values'][0]
    except IndexError as e:
        messagebox.showinfo("A T E N C I O N ! ! !", "Selecciones una consulta, por favor!")
        return
    self.windows_fichas_consulta2 = Toplevel()
    self.windows_fichas_consulta2.transient(self.windows_consulta1)
    self.windows_fichas_consulta2.resizable(width=False, height=False)
    #self.windows_fichas_consulta2.resizable(0,0)
    #   Medidas del Geometry ("ancho x alto + columna + fila")
    self.windows_fichas_consulta2.geometry("655x600+700+0") #"670x500+10+50"

    query = 'SELECT * FROM visuales WHERE ID_IMAGEN_VISUAL = ?'
    parameters = (self.miid_nombre_imagen.get(), )
    db_rows = self.run_query(query, parameters)
    for database_visuales2 in db_rows:
        self.midescripcion_imagen.set(database_visuales2[4])

    self.tarjeta_ficha_consulta1="/oftica/visuales/" + self.miid_nombre_imagen.get()
    self.tarjeta_imagen_consulta1=ImageTk.PhotoImage(file=self.tarjeta_ficha_consulta1)

    #if self.sw_elimina_imagen==0:
    #frame para menú
    self.frame200 = LabelFrame(self.windows_fichas_consulta2, text = '', foreground = 'darkgreen', font = ('arial', 16, 'bold'), background='lightcyan2')
    self.frame200.place(x=70, y=510) 

    Button(self.frame200, text = ' Escuchar', command = self.voz_imagen_consulta, background="cyan4", 
        image=self.photo_tarjeta_speaker, compound="left", height = 30, width = 150, foreground = 'black', 
        font = ('arial', 12, 'bold')).grid(row = 1, column = 0, columnspan = 1, sticky = W + E, pady=5, padx=5)
    Button(self.frame200, text = ' Zoom', command = self.zoom_imagen_consulta2, background="cyan4", 
        image=self.photo_zoom, compound="left", height = 30, width = 150, foreground = 'black', 
        font = ('arial', 12, 'bold')).grid(row = 1, column = 1, columnspan = 1, sticky = W + E, pady=5, padx=5)
    Button(self.frame200, text = ' Volver', command = lambda : self.windows_fichas_consulta2.destroy(), background="seaGreen3", 
        image=self.photo_volver_consulta, compound="left", height = 30, width = 150, foreground = 'black', 
        font = ('arial', 12, 'bold')).grid(row = 1, column = 2, columnspan = 1, sticky = W + E, pady=5, padx=5)
    Label(self.frame200, text = '' + self.midescripcion_imagen.get(), font = ('arial', 12, 'bold'), 
        background='lightcyan2').grid(row = 0, column = 0, columnspan = 5, sticky = W + E, pady=5, padx=5)


    img_frame = tk.Frame(self.windows_fichas_consulta2, height=400, width=600, bg='#faf0e6')
    img_frame.pack()
    canvas = tk.Canvas (img_frame, height=400, width=600, bg='#faf0e6', relief=tk.SUNKEN)

    sbarV = tk.Scrollbar(img_frame, orient=tk.VERTICAL, command=canvas.yview)
    sbarH = tk.Scrollbar(img_frame, orient=tk.HORIZONTAL, command=canvas.xview)
    sbarV.pack(side=tk.RIGHT, fill=tk.Y)
    sbarH.pack(side=tk.BOTTOM, fill=tk.X)

    canvas.config(yscrollcommand=sbarV.set)
    canvas.config(xscrollcommand=sbarH.set)
    canvas.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
    img = Image.open(self.tarjeta_ficha_consulta1) 
    width, height = img.size
    canvas.config(scrollregion=(0, 0, width, height))
    img2 = ImageTk.PhotoImage(img)
    self.image_on_canvas = canvas.create_image(0, 0, anchor="nw", image=img2)
    canvas.config(self.image_on_canvas)

它显示图像,没有问题,但会产生以下错误。

C:\Oftica>oftalmenu.py
Exception in Tkinter callback  
Traceback (most recent call last):
  File "C:\Users\panto\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
   File "C:\Oftica\oftalmenu.py", line 3727, in ver_imagen1
    self.visualizar_imagen_consulta()
   File "C:\Oftica\oftalmenu.py", line 3831, in   visualizar_imagen_consulta
  File "C:\Users\panto\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1646, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Users\panto\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1631, in _configure
    cnf = _cnfmerge(cnf)
  File "C:\Users\panto\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 109, in _cnfmerge
    for c in _flatten(cnfs):
TypeError: object of type 'int' has no len()

首先,当我们使用canvas.itemconfigure时,我们需要设置要更改其选项的对象的标签或Id,然后我们选择要更改的选项。在这种情况下,我认为没有必要使用 itemconfigure,因为您已经创建了要创建的图像。此外,canvas.create_image 没有名为 fill 的选项。

我已经通过两次更改解决了问题

先做Global img2

global img2
img2 = ImageTk.PhotoImage(img)

第二次更改这些行 self.image_on_canvas = canvas.create_image(0, 0, anchor="nw", image=img2) canvas.config(self.image_on_canvas)

我了解到,当使用 image_on_canvas = canvas.create_image (0, 0, anchor = "nw", image = img2) 时,它 returns 一个用于识别的 int具体的 canvas 元素(它的 id)。对于他们,只需将行更改为:canvas.create_image (0, 0, anchor = "nw", image = img2)

我的 codigo que de la siguiente manera:

    img_frame = tk.Frame(self.windows_fichas_consulta2, height=400, width=600, bg='#faf0e6')
    img_frame.pack()
    canvas = tk.Canvas (img_frame, height=400, width=600, bg='#faf0e6', relief=tk.SUNKEN)

    sbarV = tk.Scrollbar(img_frame, orient=tk.VERTICAL, command=canvas.yview)
    sbarH = tk.Scrollbar(img_frame, orient=tk.HORIZONTAL, command=canvas.xview)
    sbarV.pack(side=tk.RIGHT, fill=tk.Y)
    sbarH.pack(side=tk.BOTTOM, fill=tk.X)

    global img2
    canvas.config(yscrollcommand=sbarV.set)
    canvas.config(xscrollcommand=sbarH.set)
    canvas.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
    img = Image.open(self.tarjeta_ficha_consulta1) 
    width, height = img.size
    canvas.config(scrollregion=(0, 0, width, height))
    img2 = ImageTk.PhotoImage(img)
    canvas.create_image(0, 0, anchor="nw", image=img2)

显示图像,滚动水平和垂直滚动条,不再产生错误。