【发布时间】:2020-03-13 09:29:00
【问题描述】:
我是前几天学习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
【问题讨论】:
-
``` 文件“
”,第 35 行,在 MyWindow(tk.Tk()).mainloop() 文件“ ”,第 24 行,在 init 标签=tk.Label(self, image=self.banana,relief=tk.RAISED, bd=6) 文件“C:\ProgramData\Anaconda3 \lib\tkinter_init_.py",第 2766 行,在 init Widget.__init__(self, master, 'label', cnf, kw) File "C:\ProgramData \Anaconda3\lib\tkinter_init_.py",第 2299 行,在 init (widgetName, self._w) + extra + self._options(cnf)) TclError: image “pyimage2”不存在```
标签: tkinter