这个是报错
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Administrator\Desktop\data-PCJM\2022\M03\clear0313\main.py", line 193, in clear_All
self.new(400,300,'clear',des=0)
File "C:\Users\Administrator\Desktop\data-PCJM\2022\M03\clear0313\main.py", line 358, in new
imgd=tk.PhotoImage(file=r'.\\bg\\back3.png')
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 3545, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 3501, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open ".\\bg\\back3.png": no such file or directory
can't invoke "event" command: application has been destroyed
while executing
"event generate $w <<ThemeChanged>>"
(procedure "ttk::ThemeChanged" line 6)
invoked from within
"ttk::ThemeChanged"
Process finished with exit code 0
提示说找不到 .\bg\back3.png 文件
可是我图片很全,路径也没问题
总之我没在图片导入发现任何错误
报错的位置在下面这个函数里
def new(self,w, h, t,des=1):
global window
# global image
if des==1:
self.window.destroy()
# else:
# pass
self.windows2 = tk.Tk()
self.windows2.title(t)
self.windows2.resizable(False, False)
screenwidth =self.windows2.winfo_screenwidth() # 屏幕宽度
screenheight = self.windows2.winfo_screenheight() # 屏幕高度
width = 500
height = 400
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
imgd=tk.PhotoImage(file=r'.\\bg\\back3.png')
bg=tk.Label(self.windows2,compound=tk.CENTER,image=imgd)
bg.place(x=0,y=0)
imge = tk.PhotoImage(file=r'server-icon.png')
img = tk.Label(self.windows2, width=60, compound=tk.CENTER, image=imge, height=60)
img.place(x=0, y=0)
self.windows2.geometry('{}x{}+{}+{}'.format(w, h,x,y))
backs = tk.Button(self.windows2, text='返回', width=18, height=3, bg='#7B68EE', fg='black',
command=lambda: self.back(self.windows2))
backs.place(x=0, y=240)
choose1=tk.Checkbutton(self.windows2,text="清理回收站",onvalue=1,offvalue=0,height=1, variable =self.c,width=10,command=self.a)
choose1.place(x=75,y=10)
self.__choose_file__=[]
# for __choosed__,__keys__ in self.choosed.values(),self.choosed.keys():
# if __choosed__==1:
# __choose_file__.append(__keys__)
for __choosed__ in range(8):
if int(self.choosed[self.choose_l[__choosed__]])==1:
self.__choose_file__.append(self.choose_l[__choosed__])
clear_files=tk.Label(self.windows2,text='现在已经选择清理的文件夹:\n{}'.format(self.__choose_file__),bg='blue',fg='black',font=('等线Light',10))
clear_files.place(x=75,y=50)
cl = tk.Button(self.windows2, text='清理', width=18, height=3, bg='#7B68EE', fg='black',
command=self.clear_All) #Sdfaxsfgvsxfdd
cl.place(x=270, y=240)
self.windows2.mainloop()
然后我再下面这个地方调用了new函数
def clear_All(self):
self.windows2.destroy()
self.window5=tk.Tk()
self.window5.title('clearing')
self.window5.resizable(False, False)
screenwidth = self.window5.winfo_screenwidth() # 屏幕宽度
screenheight = self.window5.winfo_screenheight() # 屏幕高度
width = 500
height = 400
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
imgd = tk.PhotoImage(file=r'.\\bg\\back3.png')
bg = tk.Label(self.window5, compound=tk.CENTER, image=imgd)
bg.place(x=0, y=0)
imge = tk.PhotoImage(file=r'server-icon.png')
img = tk.Label(self.window5, width=60, compound=tk.CENTER, image=imge, height=60)
img.place(x=0, y=0)
# info=tk.Text(self.window5,text='{}'.format()) #herehereherehereherehereherehereherehereherehereherehere
scroll = tk.Scrollbar()
self.text = tk.Text(self.window5, width=50, height=15,bg='orange',fg='Black',wrap='char',bd=3)
scroll.place(x=350,y=100)
self.text.place(x=0, y=0)
scroll.config(command=self.text.yview)
self.text.config(yscrollcommand=scroll.set)
self.text.place(y=100)
self.text.insert(tk.INSERT,'[INFO:]start\n')
self.window5.geometry('{}x{}+{}+{}'.format(400, 300, x, y))
self.clean = clear()
a=self.get_new_default()
tm=0
with open('date.shaiden','w') as file:
for __file__ in a:
self.clean.c_Temp_d(self.lj[__file__])
self.clean.c_Temp_f(self.lj[__file__])
file.write(f'[{datetime.datetime.now()}:{"INFO"}]clearfile<{__file__}>\n')
self.text.insert(tk.INSERT, f'[INFO:]clearfile<{__file__}>\n')
self.text.insert(tk.INSERT, f'[INFO:]End for clear \n')
file.write(f'[{datetime.datetime.now()}:{"INFO"}]End for clear')
file.close()
tm=1
if tm==1:
self.window5.destroy()
print('asdasasdfsfasdsafsadffsadf')
self.new(400,300,'clear',des=0)
self.window5.mainloop()
self.window是最开始的窗口
这是一个很神奇的问题
你用的是相对路径
很可能运行时启动路径跟你最初设计时已经不一样了
你的程序是怎么打包的,怎么运行的,双击的exe吗
如果你的启动路径会变,那么别用相对路径,用os.path.dirname(__file__)获取可执行程序所在的绝对路径,再用它来拼接你想要的路径
imgd=tk.PhotoImage(file=r'.\\bg\\back3.png')
变成
self.imgd=itk.PhotoImage(file=r'.\\bg\\back3.png')