几天前我是一名学习python的学生,并且我现在对'tkinter'感兴趣。
我不知道下面的代码和错误消息中哪里出了错。
请帮助我。.TT
# -*- coding: utf-8 -*-
import tkinter as tk
banana=r'banana.gif'
bodercolor=[('aliceblue','#F0F8FF'),('blue','#000FF'),
('beige','#F5F5DC'),('cornsilk','#FFF8DC'),
('red','#ff0000'),('lightgreen','#90EE90')]
class BgChange:
def __init__(self, label, color):
self.label = label
self.color = color
def __call__(self, event=None):
self.label.configure(bg=self.color)
class MyWindow(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.master.title('select bordercolor')
f_button=tk.Frame(self)
f_button.pack(side=tk.LEFT, padx=5, pady=1)
self.banana=tk.PhotoImage(file = banana)
label=tk.Label(self, image=self.banana,relief=tk.RAISED, bd=6)
label.pack(side=tk.RIGHT,padx=7)
for name, code in bodercolor:
b=tk.Button(f_button, text=name,
bg=code, command=BgChange())
b.pack(fill=tk.X)
if __name__== '__main__':
MyWindow(tk.Tk()).mainloop()
错误消息
runfile('C:/Users/User/Desktop/python_ex/report/문제2.py', wdir='C:/Users/User/Desktop/python_ex/report')
Traceback (most recent call last):
File "<ipython-input-14-8a31f2388234>", line 1, in <module>
runfile('C:/Users/User/Desktop/python_ex/report/문제2.py', wdir='C:/Users/User/Desktop/python_ex/report')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/User/Desktop/python_ex/report/문제2.py", line 35, in <module>
win = MyWindow(root)
File "C:/Users/User/Desktop/python_ex/report/문제2.py", line 24, in __init__
label=tk.Label(self, image=self.banana,relief=tk.RAISED, bd=6)
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 2766, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 2299, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TclError: image "pyimage10" doesn't exist
答案 0 :(得分:1)
欢迎使用Stackoverflow。 您应提供一个最小可复制示例
根据您的错误:
TclError:图像“ pyimage10”不存在
我提供的是完整的\绝对路径
self.banana='C:\\Users\\Python\\banana.gif'
我还引用了这张图片:
self.master.banana= PhotoImage(file = self.banana)
这里是完整的代码:
# -*- coding: utf-8 -*-
from tkinter import Tk, Label, Frame, PhotoImage
class MyWindow():
def __init__(self, master=None):
self.banana='C:\\Users\\Python\\banana.gif'
self.master = master
self.master.title('select bordercolor')
self.f_button = Frame(self.master)
self.f_button.pack(side= "left", padx=5, pady=1)
self.master.banana= PhotoImage(file = self.banana)
self.label= Label(self.master, image=self.master.banana, relief="raised", bd=6)
self.label.pack(side="right", padx=7)
if __name__== '__main__':
root = Tk()
MyWindow(root)
root.mainloop()
您的代码中还有其他错误,但这是另一个值得解决的问题。请参阅按钮中的tkinter命令选项!
答案 1 :(得分:0)
我正在与tkinter一起工作,发现了相同的错误,但有趣的是,我注意到一种模式,例如它有时起作用并且有时会显示此错误!
最简单的解决方法是先运行以下代码:
将tkinter导入为tk
root = tk.TK()
root.mainloop()
之后将显示多个Tkinter窗口,请关闭所有窗口,然后运行您的代码 从头开始,即再次导入库,然后运行代码。
我希望这可以解决问题。