【发布时间】:2018-04-07 09:02:17
【问题描述】:
我尝试了谷歌上与我的问题相关的所有帖子,但仍然无法解决。我正在尝试将 1 张图像作为背景加载到 tkinter 正在生成的表单中。使用以下代码,我收到错误消息: 回溯(最近一次通话最后): 文件“”,第 89 行,在 AttributeError: 'Label' 对象没有属性 'image1'
下面是代码:
from tkinter import *
import tkinter as tk
from PIL import ImageTk, Image
master = Tk()
master1= Toplevel()
master.title("Crop Yield Prediction")
master.geometry('600x600')
image2 = Image.open('D:\\Pictures\\god0a.jpg')
image1 = ImageTk.PhotoImage(image2)
background_label = tk.Label(master1, image=image1)
background_label.image1
background_label.photo=background
background_label.place(x=0, y=0, relwidth=1, relheight=1)
【问题讨论】:
-
仅仅因为你在构造函数中使用了
image=image1,并不意味着它成为background_label的一个属性。如果你愿意,你应该明确地把它变成一个。事实上,tk.Label.__init__不可能使用setattr,因为它事先不知道调用者将绑定到参数的名称。
标签: python image tkinter pycharm