简体   繁体   中英

Python Tkinter: RecursionError: maximum recursion depth exceeded

I am trying to make an image viewer that is split into four frames each of which displays an image separately, I keep getting this error:

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

Here is my class:

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()

I commented out the three other frames to focus on the first one until I solve my exceptions. I have been looking into similar posted threads, and from what I could gather this is cause by the root.mainloop() function being called more than once, What am I missing please?

What happens if you remove "tk.Tk" and "tk.Frame" from class SlideshowModel(tk.Tk, tk.Frame)? Also you should replace self.grid_height by self.grid_h (line 22).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM