【问题标题】:How do I add a picture background on tkinter on a toplevel window?如何在顶层窗口的 tkinter 上添加图片背景?
【发布时间】:2019-03-14 16:11:08
【问题描述】:

我发现此图片背景代码在我的主窗口上效果很好:

C = Canvas(window, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\\file_address\\background.png")
background_label = Label(window, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.grid(row=0, column=0, rowspan=5, columnspan=3)

但是,当我在 Toplevel() 窗口上使用它时,它根本没有出现 - 我留下了灰色背景。它显示的唯一方法是当我使用 C.pack() 但我的所有小部件都没有显示时。我尝试过使用 C.lift() 和 C.lower(),但似乎没有一个能达到我想要的效果。

【问题讨论】:

    标签: python tkinter tkinter-canvas tkinter-layout


    【解决方案1】:

    正在被垃圾收集的图像参考了图像

    C = Canvas(window, bg="blue", height=250, width=300)
    filename = PhotoImage(file = "C:\\file_address\\background.png")
    background_label = Label(window, image=filename)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    
    background_label.image = filename # reference to the image
    C.grid(row=0, column=0, rowspan=5, columnspan=3)
    

    这样做会在您打开toplevel window时显示图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多