python - 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).
推荐阅读
- pandas - 是否可以使用定量变量应用 KNN?
- python - python`os.system(“xdg-open test.pdf”)`不打开文件
- twilio - 无法通过 twilio 发送短信,出现 400 错误
- lambda - 使用符号函数安装递归函数
- c++ - I want to read buffer from device that is connected to the port im using windows.h C++
- python - Is there a way to format a column in a python pandas dataframe so that negative numbers are colored red and surrounded by parentheses?
- flutter - 将多个图像从 JPG 压缩为 PNG
- python - 无法使用 python selenium 与这个动态下拉列表交互
- windows - Command::new API 如何在 Windows 上隐藏调用程序的窗口?
- typescript - 定义元组的混合数组