0

hello i try to build a simpel watermark app i user custom tkinter for user interface ( its same to tkinter )

when i upload a picture with button my function everything work expact picture in canvas i see the canvas but i dont see a picture i test the code with a text on canvas and its show when i add a label with same photo ( i comment that line in this cod) even i dont grid it on window my picture show in canvas this is my cod :

import customtkinter
from PIL import Image, ImageTk


my_font1 = ('times', 18, 'bold')
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")
window = customtkinter.CTk()
window.title("Water mark Photo")
window.config(padx=50, pady=50)
window.geometry("600x600")
input_pic_label = customtkinter.CTkLabel(master=window,
                                     text="Choose Your Photo",
                                     font=my_font1)
input_pic_label.grid(row=0, column=0, padx=(180, 0), pady=(200, 0))

choose_pic_button = customtkinter.CTkButton(master=window,
                                        text="Upload Photo",
                                        command=lambda: upload_file())
choose_pic_button.grid(row=1, column=0, padx=(180, 0))


def upload_file():
    choose_pic_button.destroy()
    input_pic_label.destroy()
    f_types = [('Jpg Files', '*.jpg'), ('Png Files', '*.png')]
    filename = customtkinter.filedialog.askopenfilename(filetypes=f_types)
    image = Image.open(filename)
    original_image_size = image.size
    print(original_image_size)
    resized_image = image.resize((500, 500))
    resized_image.save("temp.jpg")
    new_image = ImageTk.PhotoImage(resized_image)
    canvas = customtkinter.CTkCanvas(master=window, width=500, height=500, highlightthickness=0,     bg="black")

    canvas.grid(row=2, column=1, columnspan=2)
    canvas.create_image(250, 250, image=new_image)
    window.update()
    # label = customtkinter.CTkLabel(master=window, text="", image=new_image)


window.mainloop()

i expect to see a picture but its show nothing i also try to di it in tkinter with the blow cod but output its same expect i get nothing as outpout here without any error:

import tkinter
from PIL import Image, ImageTk
from tkinter import filedialog

my_font1 = ('times', 18, 'bold')

window = tkinter.Tk()
window.title("Water mark Photo")
window.config(padx=50, pady=50)

input_pic_label = tkinter.Label(window,
                                     text="Choose Your Photo",
                                     font=my_font1)
input_pic_label.grid(row=0, column=0, padx=(180, 0))

choose_pic_button = tkinter.Button(window,
                                        text="Upload Photo",
                                        command=lambda: upload_file())
choose_pic_button.grid(row=1, column=0, padx=(180, 0))
canvas = tkinter.Canvas(window, width=500, height=500, highlightthickness=0)
text = canvas.create_text(250, 250, text="Your image")
canvas.grid(row=2, column=1, columnspan=2)

def upload_file():
    #choose_pic_button.destroy()
    #input_pic_label.destroy()
    #canvas.itemconfig(text, text="")
    f_types = [('Jpg Files', '*.jpg'), ('Png Files', '*.png')]
    filename = tkinter.filedialog.askopenfilename(filetypes=f_types)
    image = Image.open(filename)
    img = ImageTk.PhotoImage(Image.open(filename))
    original_image_size = image.size
    print(original_image_size)
    resized_image = image.resize((500, 500))
    resized_image.save("temp.jpg")
    resized_image = ImageTk.PhotoImage(resized_image)

    canvas.create_image(250, 250, image=resized_image)

    label = tkinter.Label(window, text="helloooooo", image=resized_image)

    label.grid(row=2, column=4)



window.mainloop()
Pourya
  • 13
  • 3
  • you may have very common problem with `bug` in `PhotoImage` which removes image when you assign to local variable in function. You have to assign `PhotoImage` to global variable. You can find this problem in many questions on StackOverflow – furas Jan 22 '23 at 17:27

0 Answers0