在要求我查看其他主题之前,我只想说我有,所有解决方案都涉及调用tk.Toplevel()
而不是tk.TK()
或位于同一.py文件中。我尝试过(也许用HAS(tk.Tk)
代替HAS(tk.Toplevel)
可能做错了。
我的代码中出现TclError: image "brand_logo.png" doesn't exist
错误。我正在开发带有一些窗口的Tkinter软件,以供用户浏览。每个窗口都会有一组要回答的问题。为了维护方便,我将每个窗口都开发在一个不同的文件中,以便可以追溯更容易的错误以及对窗口本身的更改。
我的窗口创建类的代码是:
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()
我的每个调查表都采用相同的结构,只是更改了文字和问题:
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
有人可以帮助我吗?
谢谢!
答案 0 :(得分:2)
我实际上无法重现该错误,您的代码对我来说没有任何图像,但是我确实发现您的代码有两个问题。
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
关键字参数:
Logo = tk.PhotoImage(file="brand_logo.png")
这样一来,Logo
仍然是__init__
函数的局部变量,因此返回后,Logo
将被垃圾回收。为防止这种情况,请将其重命名为self.Logo
,以使其成为实例变量。