无法使用 Tkinter 显示背景图像

Unable to display background image using Tkinter

问题是当我 运行 这段代码时,按钮、标签都显示出来了。但只有背景图像没有。我不知道它发生了什么。我试图参考互联网上可用的解决方案,但没有找到解决方案。反正这段代码可以运行不报错。请帮助我。

import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
import numpy as np




class ProcessImage_window:
    def __init__(self, master):  # parent, *args, **kwargs):
        self.master = master
        self.frame = tk.Frame(self.master)

        # Define images and icons
        self.bg = ImageTk.PhotoImage(Image.open('C:\xampp\htdocs\PyFYP\images\neon.png'))


        # OpenCV window name
        self.windowName = "Selected Image"

        # Background image
        self.Img_process_background = Label(master, image=self.bg)
        self.Img_process_background.place(x=0, y=0, relwidth=1, relheight=1)

        # Attributes for setting coordinates
        self.coordinate = np.zeros((3, 2), int)
        self.counter = 0
        self.file_path_name = ""  # Uploaded image's file path

        # Labels and Buttons

        # Upload Image label and entry
        self.uploadImg_label = Label(master, text="Please upload your preference image here.", bg="#1f1a30",
                                     font=("arial", 12, "bold"), fg="white")
        self.uploadImg_label.place(x=50, y=50)
        self.uploadImg_note = Label(master, text="* Only jpg, jpeg, png, are allowed.", bg="#1f1a30",
                                    font=("arial", 8, "bold"), fg="grey")
        self.uploadImg_note.place(x=50, y=75)
        self.img_resize_note = Label(master, text="* Image will be resize to 900x500.", bg="#1f1a30",
                                     font=("arial", 8, "bold"), fg="grey")
        self.img_resize_note.place(x=50, y=100)
        self.txt_uploadImg = Entry(master, font=("arial", 10), bg="#39304d", fg="white", borderwidth=0)
        self.txt_uploadImg.place(x=50, y=125, width=350, height=20)


def main_image_process():
    imageprocess_page = tk.Tk()
    imageprocess_page.title("Image processing")

    imageprocess_page.iconbitmap('C:\xampp\htdocs\PyFYP\Icon\snake2.ico')
    imageprocess_page.resizable(False, False)

    app_width = 900
    app_height = 500

    screen_width = imageprocess_page.winfo_screenwidth()
    screen_height = imageprocess_page.winfo_screenheight()

    sys_width = (screen_width / 2) - (app_width / 2)
    sys_height = (screen_height / 2) - (app_height / 2)
    imageprocess_page.geometry(f'{app_width}x{app_height}+{int(sys_width)}+{int(sys_height)}')
    ProcessImage_window(imageprocess_page)
    imageprocess_page.mainloop()


if __name__ == "__main__":
    main_image_process()

这是因为您没有对 ProcessImage_window() 实例的引用,所以没有外部引用的项目,如 self.bg,将被垃圾回收。

保存 ProcessImage_window() 的引用:

def main_image_process():
    imageprocess_page = tk.Tk()
    imageprocess_page.title("Image processing")

    imageprocess_page.iconbitmap('C:\xampp\htdocs\PyFYP\Icon\snake2.ico')
    imageprocess_page.resizable(False, False)

    app_width = 900
    app_height = 500

    screen_width = imageprocess_page.winfo_screenwidth()
    screen_height = imageprocess_page.winfo_screenheight()

    sys_width = (screen_width / 2) - (app_width / 2)
    sys_height = (screen_height / 2) - (app_height / 2)
    imageprocess_page.geometry(f'{app_width}x{app_height}+{int(sys_width)}+{int(sys_height)}')
    app = ProcessImage_window(imageprocess_page) # save a reference
    imageprocess_page.mainloop()