【问题标题】:Load background image with Tkinter GUI使用 Tkinter GUI 加载背景图像
【发布时间】: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


【解决方案1】:

对代码进行了一些更改,并且成功了!!

image2 = Image.open('D:\\Pictures\\god0a.jpg')
#image2.show()
image1 = ImageTk.PhotoImage(image2)
background_label = tk.Label(master, image=image1)
background_label.image1=image1
background_label.place(x=0, y=0, height=250, width=350)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多