【问题标题】:Python Tkinter: RecursionError: maximum recursion depth exceededPython Tkinter:RecursionError:超出最大递归深度
【发布时间】:2021-02-07 19:55:45
【问题描述】:

我正在尝试制作一个分成四个帧的图像查看器,每个帧分别显示一个图像,我不断收到此错误:

Traceback (most recent call last):
  File "c:/Users/EM/Desktop/Scripts/gui/slideshow_model/slide_show_class.py", line 60, in <module>
    slideshow_model = SlideshowModel(root)
  File "c:/Users/EM/Desktop/Scripts/gui/slideshow_model/slide_show_class.py", line 22, in __init__
    self.frame_1, width=self.grid_w, height=self.grid_height)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2098, in __getattr__
    return getattr(self.tk, attr)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2098, in __getattr__
    return getattr(self.tk, attr)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2098, in __getattr__
    return getattr(self.tk, attr)
  [Previous line repeated 494 more times]
RecursionError: maximum recursion depth exceeded

这是我的课:

import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()


class SlideshowModel(tk.Tk, tk.Frame):

    def __init__(self, master):
        self.master = root
        master.title('Basic Image Viewer')
        root.iconbitmap('../img/favicon.ico')
        root.state('zoomed')
        s_w = int(root.winfo_screenwidth())
        s_h = int(root.winfo_screenheight())
        self.grid_w = s_w // 2
        self.grid_h = s_h // 2
        self.frame_1 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0)
        self.frame_1.grid(column=0, row=0)
        self.canvas_1 = tk.Canvas(
            self.frame_1, width=self.grid_w, height=self.grid_height)
        #self.frame_2 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        #self.frame_2.grid(column=1, row=0)
        #self.frame_3 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        #self.frame_3.grid(column=0, row=1)
        #self.frame_4 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        #self.frame_4.grid(column=1, row=1)

    # show image function
    # should contain grid coordinates
    # image dimensions calculator
    # should return the image object
    def resize_image(self, img_path):
        image = Image.open(img_path)
        w_coeff = image.width / self.grid_w
        h_coeff = image.height / self.grid_h
        w_coeff = 1 / w_coeff if w_coeff > 1 else w_coeff
        h_coeff = 1 / h_coeff if h_coeff > 1 else h_coeff
        # pick the smallest coeff to get the image as small
        # as should be
        coeff = min(w_coeff, h_coeff)
        image = image.resize(
            (int(image.width * coeff), int(image.height * coeff)), Image.ANTIALIAS)
        return image

    # this function should show returned image
    # takes: image object, master frame
    def show_image(self, resize_image, frame_x):
        image = ImageTk.PhotoImage(resize_image)
        # label = tk.Label(frame_x, image=image, bd=0)
        self.canvas_1(image=image, anchor='nw')


slideshow_model = SlideshowModel(root)

img1 = slideshow_model.resize_image('../img/sample.jpg')
# img2 = slideshow_model.resize_image('../img/sample.jpg')
# img3 = slideshow_model.resize_image('../img/sample.jpg')
# img4 = slideshow_model.resize_image('../img/sample.jpg')

slideshow_model.show_image(img1, slideshow_model.frame_1)
# slideshow_model.show_image(img2, slideshow_model.frame_2)
# slideshow_model.show_image(img3, slideshow_model.frame_3)
# slideshow_model.show_image(img4, slideshow_model.frame_4)

root.mainloop()

我注释掉了其他三个框架以专注于第一个框架,直到我解决了我的异常。我一直在研究类似的帖子,据我所知,这是因为root.mainloop() 函数被多次调用,请问我错过了什么?

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    如果从类 SlideshowModel(tk.Tk, tk.Frame) 中删除“tk.Tk”和“tk.Frame”会发生什么?此外,您应该将 self.grid_height 替换为 self.grid_h(第 22 行)。

    【讨论】:

    • @Mikhail_llin 现在似乎没有显示异常,但是我收到了 self.canvas_1(image=image, anchor='nw') TypeError: 'Canvas' object is not callable 阻止了我,我不知道当这个问题得到解决时,maximum recursion depth 问题会回来的。
    • 可能你想调用 self.canvas_1.create_image(50, 50, anchor=NW, image=image) 为此你还需要“从 tkinter 导入 N、S、E、W”如果我是你,我会首先尝试编写没有自定义类的代码,一旦它工作,将它包装到一个类中。我的印象是类的创建增加了不必要的复杂性。
    猜你喜欢
    • 2011-12-31
    • 1970-01-01
    • 2020-07-13
    • 2017-03-18
    • 2015-04-15
    • 1970-01-01
    • 2013-12-01
    • 2021-12-11
    相关资源
    最近更新 更多