Python Tkinter:递归错误:超过了最大递归深度



我正在尝试制作一个图像查看器,它被分成四个帧,每个帧分别显示一个图像,我一直收到这个错误:

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:UsersEMAppDataLocalProgramsPythonPython37libtkinter__init__.py", line 2098, in __getattr__
return getattr(self.tk, attr)
File "C:UsersEMAppDataLocalProgramsPythonPython37libtkinter__init__.py", line 2098, in __getattr__
return getattr(self.tk, attr)
File "C:UsersEMAppDataLocalProgramsPythonPython37libtkinter__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()函数被多次调用,请问我缺少什么?

如果删除"tk.tk";以及";tk.Frame";来自SlideshowModel(tk.tk,tk.Frame(类?此外,您应该将self.grid_height替换为self.grid_h(第22行(。

最新更新