The problem is when I run this code, the buttons, the labels, are showed up. But only the background image doesn't. I do not know what happen to it. I tried to refer to solution available on the internet, but did not find solution. Anyway, this code can be run without error. Please help me.
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()
CodePudding user response:
It is because you don't have a reference to the instance of ProcessImage_window()
so items without external reference, like self.bg
, will be garbage collected.
Save a reference of 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()