TclError:图像不存在-Tkinter多窗口python [英] TclError: image doesn't exist - Tkinter multi-windows python

查看:91
本文介绍了TclError:图像不存在-Tkinter多窗口python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在要求我查看其他主题之前,我只想说我有,所有解决方案都涉及调用 tk.Toplevel()而不是 tk.TK()或位于同一.py文件中.我尝试过(也许用 HAS(tk.Tk)代替 HAS(tk.Toplevel)做错了.

Before I am asked to look over other topics, I just want to say that I have and all of the solutions either involve calling a tk.Toplevel() instead of tk.TK() or are within the same .py file. I have tried it (perhaps done wrongly by substituting HAS(tk.Tk) for HAS(tk.Toplevel).

我收到错误 TclError:我的代码中图像"brand_logo.png"不存在.我正在开发带有一些窗口的Tkinter软件,以供用户浏览.每个窗口都会有一组要回答的问题.为了便于维护,我将每个窗口都开发在一个不同的文件中,以便可以追溯更容易的错误以及对窗口本身的更改.

I am getting an error of TclError: image "brand_logo.png" doesn't exist within my code. I am developing a Tkinter software with some windows to the user to navigate through. Each window will have a set of questions to be answered. For easiness of maintenance, I developed each window in a different file so I can track back easier errors and changes to the windows themselves.

我的窗口创建类的代码是:

The code for my window creation class is:

import tkinter as tk, Questions1 as q1, Questions2 as q2
class HAS(tk.Tk):    
def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, *kwargs)
    #Page configure
    tk.Tk.wm_title(self, "Checklist")
    container = tk.Frame(self)
    container.pack(side="top", fill="both", expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)
    container.configure(background = "white")

    #Adding overhead menu
    menubar = tk.Menu(container)
    filemenu = tk.Menu(menubar, tearoff = 0)
    filemenu.add_command(label = "Save", command = self.saveList)
    filemenu.add_command(label = "Exit", command= self.destroy)
    menubar.add_cascade(label="File", menu=filemenu)
    tk.Tk.config(self, menu=menubar)

    #Defining the page_frames
    self.frames = {}
    for F in (q1.Q1, q2.Q2):
        frame = F(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")
    self.show_frame(q1.Q1)

#showing the selected frame              
def show_frame(self, cont):
    frame = self.frames[cont]
    frame.tkraise()

# Function accessing the list of answers
def saveList(self):
    dict_answers={'Answers 1': self.frames[q1.Q1].list_answers, "Answers 2": self.frames[q2.Q2].list_answers}
    print(dict_answers)

app = HAS() 
app.geometry("530x700")
app.mainloop()

我的每个调查表都采用相同的结构,只是更改了文字和问题:

and each of my questionnaires follow the same structure, just changing the text and questions:

class Q1(tk.Frame):
def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.columnconfigure(0, minsize=100)
    self.columnconfigure(1, minsize=150)
    self.columnconfigure(2, minsize=50)     
    Logo = tk.PhotoImage("brand_logo.png")
    #Header Config
    tk.Frame.configure(self, background = "white")
    ttk.Label(self, image=Logo, background="white").grid(row=0, column=0, padx=100, columnspan=3, pady=10, sticky="W")
    ttk.Label(self, text="1st Checklist", font = FONT, background = "white").grid(row=1, column=0, columnspan=3, pady = 10)
    ttk.Label(self, text="Questions: ", background="white").grid(row=2, column=0,columnspan=2, padx=10, pady = 10, sticky="W")

    #Questions
    global qt
    qt = [tk.StringVar(self) for i in range(len(Questionlist_a))]
    for r in range(len(Questionlist_a)): 
        ttk.Label(self, text=Questionlist_a[r], background = "white").grid(row= r+3, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
        ttk.OptionMenu(self, qt[r], *choices_y_n).grid(row = r+3, column=2, padx=10, sticky="WE")
        r=+1

    #Buttons  
    ttk.Button(self, text="Save values", command = self.save_values, width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")
    ttk.Button(self, text="Questions 2", command = lambda: controller.show_frame(__import__('Questions2').Q2),width=18).grid(row=17, column=0, padx=10, pady=15, sticky="W")

    # List of answers
    self.list_answers=[]

def save_values(self):
    self.list_answers = list(map(lambda x: x.get(), qt))

完整的回溯错误是:

Traceback (most recent call last):
File "<ipython-input-3-69ca257aaadd>", line 1, in <module>
runfile('C:/Users/nlcaste9/Desktop/New folder/Main.py', wdir='C:/Users/nlcaste9/Desktop/New folder')

File "\WPy-3662\python-3.6.6.amd64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)

File "C:\Users\nlcaste9\Downloads\WPy-3662\python-3.6.6.amd64\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/nlcaste9/Desktop/New folder/Main.py", line 49, in <module>
app = HAS()

File "C:/Users/nlcaste9/Desktop/New folder/Main.py", line 33, in __init__
frame = F(container, self)

File "C:\Users\nlcaste9\Desktop\New folder\Questions1.py", line 25, in __init__
ttk.Label(self, image=Logo, background="white").grid(row=0, column=0, padx=100, columnspan=3, pady=10, sticky="W")

File "C:\Users\nlcaste9\Downloads\WPy-3662\python-3.6.6.amd64\lib\tkinter\ttk.py", line 761, in __init__
Widget.__init__(self, master, "ttk::label", kw)

File "C:\Users\nlcaste9\Downloads\WPy-3662\python-3.6.6.amd64\lib\tkinter\ttk.py", line 559, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)

File "C:\Users\nlcaste9\Downloads\WPy-3662\python-3.6.6.amd64\lib\tkinter\__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf))

TclError: image "brand_logo.png" doesn't exist

有人可以帮助我吗?

谢谢!

推荐答案

我实际上无法重现该错误,您的代码对我而言未显示任何图像,但我确实发现您的代码存在两个问题.

I actually can't reproduce the error, your code just doesn't show any image for me, but I do see two problems with the code you have.

PhotoImage __ init __ 方法定义为

class PhotoImage(Image):
    """Widget which can display colored images in GIF, PPM/PGM format."""
    def __init__(self, name=None, cnf={}, master=None, **kw):
        """Create an image with NAME.

        Valid resource names: data, format, file, gamma, height, palette,
        width."""
        Image.__init__(self, 'photo', name, cnf, master, **kw)

这意味着第一个参数是 name .因此,当您调用 Logo = tk.PhotoImage("brand_logo.png")时,您将创建一个名称为 brand_logo.png 的图像,但实际上并未指定应使用哪个图像文件使用.要指定图像文件,请使用 file 关键字参数:

This means that the first argument is name. So when you call Logo = tk.PhotoImage("brand_logo.png"), you create an image with the name brand_logo.png, but without actually specifying which image file should be used. To specify the image file use the file keyword argument:

Logo = tk.PhotoImage(file="brand_logo.png")

这样, Logo 仍然是 __ init __ 函数的局部变量,因此返回后, Logo 将是垃圾收集.为了防止这种情况,请通过将其重命名为 self.Logo 到各处来将其设置为实例变量.

With that out of the way, Logo is still a local variable to the __init__ function, so after that returns, Logo will be garbage collected. To prevent this, make it an instance variable by renaming it to self.Logo everywhere.

这篇关于TclError:图像不存在-Tkinter多窗口python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆