|  | 
 
| 
各位大神,我跟着小甲鱼老师的案例打,但是却报错了
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册    (我保证 1.gif 是在程序的同级目录下的)
 
 代码:
 
 复制代码from tkinter import *
root = Tk()
def callback():
    var.set("我才不信呢~")
root = Tk()
frame1 = Frame(root)
frame2 = Frame(root)
# 创建一个 Label 对象
var = StringVar()
var.set("从入门\n到放弃")
textLabel = Label(frame1, textvariable=var, justify=LEFT)
textLabel.pack(side=LEFT)
# 创建一个图像 Label 对象
photo = PhotoImage(file="1.gif")
imgLabel = Label(frame1, image = photo)
imgLabel.pack(side=RIGHT)
# 添加一个按钮
theButton = Button(frame2, text = "我不会放弃的^_^", command=callback)
theButton.pack()
frame1.pack(padx=10, pady=10)
frame2.pack(padx=10, pady=10)
mainloop()
报错信息
 Traceback (most recent call last):
 File "C:\Users\lzg99\Desktop\鱼C\python\随便学\tkinter\tk-4.py", line 21, in <module>
 imgLabel = Label(frame1, image = photo)
 File "C:\Users\lzg99\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
 Widget.__init__(self, master, 'label', cnf, kw)
 File "C:\Users\lzg99\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
 self.tk.call(
 _tkinter.TclError: image "pyimage1" doesn't exist
 
去掉一个 root = Tk() 即可 
因为在一个程序中只能存在一个根窗口
 复制代码from tkinter import *
root = Tk()
def callback():
    var.set("我才不信呢~")
frame1 = Frame(root)
frame2 = Frame(root)
# 创建一个 Label 对象
var = StringVar()
var.set("从入门\n到放弃")
textLabel = Label(frame1, textvariable=var, justify=LEFT)
textLabel.pack(side=LEFT)
# 创建一个图像 Label 对象
photo = PhotoImage(file="1.png")
imgLabel = Label(frame1, image = photo)
imgLabel.pack(side=RIGHT)
# 添加一个按钮
theButton = Button(frame2, text = "我不会放弃的^_^", command=callback)
theButton.pack()
frame1.pack(padx=10, pady=10)
frame2.pack(padx=10, pady=10)
mainloop()
 | 
 |