Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13921

2 Python Classes 1 File -> CustomTkinter Image cannot be loaded

$
0
0

In case of a project for our university, we tried to create an Adblocker. My Task is to create a UI for our blocking software. Therefore, I'm using the Customtkinter library.

I created 2 Classes, the first is the UI itself, the second one the welcome page. On the welcome page, the user has to insert the IP Address of the database our project needs.

How it works:

→ User starts the application → Welcome page appears → User enters IP → UI appears.

Because I didn't find another solution, I created a normal CTK Window for both classes.

class UserInterface(customtkinter.CTk):    def __init__(self):        super().__init__()        self.geometry("600x600")        self.title("CTk example")
class Welcomepage(customtkinter.CTk):    def __init__(self):        super().__init__()        self.geometry("384x308")        self.title("Welcome")

Both are defined in the Application.py located in the main folder of the project.

I also want to use images, so I created the folder images where all images are located and images.py also located in this folder.

Images.py just defines all images:

import customtkinterfrom PIL import Image# Menu-bar Image:logo_ui = customtkinter.CTkImage(light_image=Image.open("images\yblocker.png"), dark_image=Image.open("images\yblocker.png"),size=(128,128))# Welcomepage Image:logo_welcome = customtkinter.CTkImage(light_image=Image.open("images\yblocker.png"), dark_image=Image.open("images\yblocker.png"),size=(128,128))

Now I want to create a label with an Image in both classes:

import images.image as imagesclass UserInterface(customtkinter.CTk):    def __init__(self):        super().__init__()        self.geometry("600x600")        self.title("CTk example")        # Initalizing Menu-bar:        self.menu = customtkinter.CTkFrame(self, width=150, height=600, border_width=1, border_color="#1F538D")        self.menu.place(x=0,y=0)        # --> Initalizing Elements of the Menu-bar:        # Image-label:        self.logo_label = customtkinter.CTkLabel(self.menu, image= images.logo_ui, text="")        self.logo_label.place(x=11,y=10)class Welcomepage(customtkinter.CTk):    def __init__(self):        super().__init__()        self.geometry("384x308")        self.title("Welcome")        self.image_label = customtkinter.CTkLabel(self, width=128,text="", image= images.logo_welcome)        self.image_label.place(x=128,y=0)

So using the image in class User interface works without any Problems (the image is visible), using the same way to show the image in class Welcomepage following Error occurs:

Traceback (most recent call last):  File "Path to Application.py", line 490, in <module>    welcome = Welcomepage()              ^^^^^^^^^^^^^  File "Path to Application.py", line 432, in __init__    self.image_label = customtkinter.CTkLabel(self, width=128,text="", image= images.logo_welcome)                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\***\AppData\Local\Programs\Python\Python312\Lib\site-packages\customtkinter\windows\widgets\ctk_label.py", line 104, in __init__    self._update_image()  File "C:\Users\***\AppData\Local\Programs\Python\Python312\Lib\site-packages\customtkinter\windows\widgets\ctk_label.py", line 141, in _update_image    self._label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),  File "C:\Users\***\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1716, in configure    return self._configure('configure', cnf, kw)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\***\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1706, in _configure    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))_tkinter.TclError: image "pyimage11" doesn't exist

Application.py:

import images.image as imagesimport customtkinterclass UserInterface(customtkinter.CTk):    def __init__(self):        super().__init__()        self.geometry("600x600")        self.title("CTk example")        # Initalizing Menu-bar:        self.menu = customtkinter.CTkFrame(self, width=150, height=600, border_width=1, border_color="#1F538D")        self.menu.place(x=0,y=0)        # Image:        self.logo_label = customtkinter.CTkLabel(self.menu, image= images.logo_ui, text="")        self.logo_label.place(x=11,y=10)class Welcomepage(customtkinter.CTk):    def __init__(self):        super().__init__()        self.geometry("384x308")        self.title("Welcome")        self.image_label = customtkinter.CTkLabel(self, width=128,text="", image= images.logo_welcome)        self.image_label.place(x=128,y=0)        self.start_button = customtkinter.CTkButton(self, width=50, text="Start", command = self.start_button_action)        self.start_button.place(x=233,y=230)    def start_button_action(self):        ui.mainloop()ui = UserInterface()wp = Welcomepage()wp.mainloop()

Here is the whole problem as a zip file (including the pictures)

So now my Question is why? I already don't understand why he's talking from "pyimage11"

  • First thing I tried was to use exactly the same image, but that changed nothing

  • Then I tried to configure the labels with the images in Images.py by first importing application.py and then configuring the respective element with .configure(image=). However, I had the problem that I naturally imported in a circle, as I have to start a function in welcomepage to do the configuration in image.py

  • with the help of AI, I tried other things, but surprisingly none of them worked...


Viewing all articles
Browse latest Browse all 13921

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>