【问题标题】:Backround Image Not Appearing in Tkinter背景图像未出现在 Tkinter 中
【发布时间】:2021-03-13 00:51:59
【问题描述】:

这是我的一个学校项目的代码,旨在制作一个应用来订购披萨。运行代码后,我似乎无法显示背景图像,我尝试将“root”切换为“frame”,但它的响应为“name 'frame' is not defined”。

import tkinter as tk

# Window
root=tk.Tk()
root.title(" Mastrella's Mozzarella Pizza's ")

# Canvas
canvas = tk.Canvas(root,height = 600 , width = 1000)
canvas.pack()

backround_image = tk.PhotoImage(file='wallpaper.png')
backround_label = tk.Label(root, image=backround_image)
backround_label.place(relwidth=1, relheight=1)

# Frame
frame = tk.Frame(root)
#frame.place(relwidth = 1, relheight = 1)
frame.place(relwidth = 1, relheight = 1)

# Label
label = tk.Label(frame, text = "Mastrella's Mozzarella Pizza's", bg = 'black', fg = 'white',  width=200, height=2)
label.config(font=("Courier", 35))
label.pack()

# Button
button = tk.Button(root, text = "Order Pizza", bg = 'red',fg = 'white')
button.config(font=("Courier", 20))
button.pack(side = 'bottom')

# pizza size backround label
label = tk.Label(frame, bg = 'red', height=10, width=16)
label.place(x = 10, y = 145)

# toppings backround label
label = tk.Label(frame, bg = 'red', height=20, width=15)
label.place(x = 190, y = 150)

# pizza size
label = tk.Label(frame, text = "Pizza Size", bg = 'black', fg = 'white', height=1, width=10)
label.config(font=("Courier", 15))
label.place(x = 20, y = 160)

# pizza sizes
checkButtonMushroom = tk.Checkbutton(frame, text = "Large", bg = 'black', fg = 'grey')
checkButtonMushroom.place(x = 20, y = 200)

checkButtonMushroom = tk.Checkbutton(frame, text = "Medium", bg = 'black', fg = 'grey')
checkButtonMushroom.place(x = 20, y = 230)

checkButtonMushroom = tk.Checkbutton(frame, text = "Small", bg = 'black', fg = 'grey')
checkButtonMushroom.place(x = 20, y = 260)

# Label
label = tk.Label(frame, text = "Toppings", bg = 'black',fg = 'white', height=1, width=10)
label.config(font=("Courier", 15))
label.place(x = 200, y = 160)

# toppings
checkButtonMushroom = tk.Checkbutton(frame, text = "Mushrooms", bg = 'black', fg = 'grey')
checkButtonMushroom.place(x = 200, y = 200)

checkButtonOlives = tk.Checkbutton(frame, text = "Olives", bg = 'black', fg = 'grey')
checkButtonOlives.place(x = 200, y = 230)

checkButtonPepperoni = tk.Checkbutton(frame, text = "Pepperoni", bg = 'black', fg = 'grey')
checkButtonPepperoni.place(x = 200, y = 260)

checkButtonHotPeppers = tk.Checkbutton(frame, text = "Hot Peppers", bg = 'black', fg = 'grey')
checkButtonHotPeppers.place(x = 200, y = 290)

checkButtonExtraCheese = tk.Checkbutton(frame, text = "Extra Cheese", bg = 'black', fg = 'grey')
checkButtonExtraCheese.place(x = 200, y = 320)

checkButtonBacon = tk.Checkbutton(frame, text = "Bacon", bg = 'black', fg = 'grey')
checkButtonBacon.place(x = 200, y = 350)

checkButtonHam = tk.Checkbutton(frame, text = "Ham", bg = 'black', fg = 'grey')
checkButtonHam.place(x = 200, y = 380)

checkButtonOnion = tk.Checkbutton(frame, text = "Onion", bg = 'black', fg = 'grey')
checkButtonOnion.place(x = 200, y = 410)

root.mainloop()

当我运行这个程序时,背景图像不显示,我不知道如何解决这个问题。我该如何解决这个问题?

【问题讨论】:

  • 框架位于背景图像的顶部并且大小相同。除非框架是透明的,否则背景不会透出来。我不知道 Tk 是否甚至提供透明窗口。
  • 我发现问题是我的框架在背景之后,切换它们解决了问题
  • 乔丹:如果你这样做了,你还能看到其他的东西吗?

标签: python python-3.x image tkinter


【解决方案1】:

只需重新排列您的代码,以便定义 frame,然后将 root 替换为 frame

# Rest of your code above

frame = tk.Frame(root)
frame.place(relwidth = 1, relheight = 1)

backround_image = tk.PhotoImage(file='wallpaper.png')
backround_label = tk.Label(frame, image=backround_image) # Use frame instead of root
backround_label.place(relwidth=1, relheight=1)

# Rest of your code below

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 2012-07-05
    • 2019-02-26
    • 1970-01-01
    相关资源
    最近更新 更多