image not showing in customtkinter :(
i dont know why its not working i think its cuse of its in Function

code : https://www.toptal.com/developers/hastebin/manaxujufu.py

https://i.stack.imgur.com/c5X9r.png

Recommended Answers

All 3 Replies

In the future, please post the code into your message (using the Code Block button, </>, in the editor window menubar), rather than as a link to a copy on another site.

Also, please tell us what is failing, in detail. We need to know what you expected the code to do, what it is actually doing, and what error messages you are getting, if any. We are not mind readers, nor are we likely to try running your code for you.

That having been said, I did try running the code, and got the following results:

$ python my_captcha.py 
lM9sZE
Traceback (most recent call last):
  File "/home/schol-r-lea/Documents/Programming/Projects/Quick Tests/my_captcha.py", line 99, in <module>
    captcha("B")
  File "/home/schol-r-lea/Documents/Programming/Projects/Quick Tests/my_captcha.py", line 73, in captcha
    render = ImageTk.PhotoImage(Image.open(dir + f"/CAPTCHA-{captcha_text}.png").resize((280, 90), Image.Resampling.LANCZOS))
  File "/usr/lib/python3.10/site-packages/PIL/Image.py", line 3092, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpkuasr8qv/CAPTCHA-lM9sZE.png'

Subsequent runs make it clear that you are trying to randomly generate a temporary directory path and image file name, but you do not seem to be actually creating file in question. Is it safe to assume that this is the error you are getting, or is it something different?

EDIT: I think I've found the problem I was getting. In the line

  image.write(captcha_text, dir + f"\\CAPTCHA-{captcha_text}.png")

You are trying to insert a Windows-style backslash path. Regardless of the OS, paths in Python should always follow the Unix format:

  image.write(captcha_text, f"{dir}/CAPTCHA-{captcha_text}.png")

I suspect that this is not the problem you are referring to, however. When I go to run the program, I do get an image similar to the one you linked to, but without the random string which the user is mean to view. Is that the problem you are trying to fix?

As a proof-of-concept, minimal reproducible example, I stripped out all of the code except that which is necessary for displaying the Captcha image:

from PIL import Image, ImageTk  
import os,re,requests,time,customtkinter
import customtkinter as ctk
from tempfile import gettempdir, mkdtemp
from captcha.image import ImageCaptcha

app = customtkinter.CTk()  # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
app.geometry("500x400")
app.title("Image Test")
PATH = os.path.dirname(os.path.realpath(__file__))

captcha_width = 280
captcha_height = 90

image = ImageCaptcha(width = captcha_width, height = captcha_height)
captcha_text = 'Test Image'
captcha = image.generate(captcha_text)
dir = mkdtemp()

image.write(captcha_text, f"{dir}/CAPTCHA-{captcha_text}.png")
render = ImageTk.PhotoImage(Image.open(f"{dir}/CAPTCHA-{captcha_text}.png").resize((captcha_width, captcha_height), Image.Resampling.LANCZOS))

img = ctk.CTkLabel(app, image=render)

img.place(relx=0.5, rely=0.35, anchor='center')
app.mainloop()

This example version does in fact display the CAPTCHA image, which leads me to think that the fault is in some interaction with the rest of the display code.

With a little further testing by a process of elimination, I have found that the critical sections are the definitions of login_load_button and captcha_input, both of which are the only user input widgets on the window. I've also found that with those two commented out (along with their relevant .place() method invocations), the CAPTCHA image appears, but after a few seconds vanishes. Just what this indicates isn't clear to me. Some issue with how it refreshes, perhaps?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.