Python Forum
[Tkinter] image doesnt exists into personal module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] image doesnt exists into personal module
#1
Hi all...

I create an interface tkinter, with python3. (3.6.2 on OpenBSD)

My folder:
Quote:yup.tk.py
/modules/__init__.py (empty)
/modules/window_about.py

the script 'yup.tk.py' call script 'window_about.py'.

into script 'yup.tk.py', i use module PIL to insert image into tk.Label, as:
import tkinter as tk
import tkinter.ttk as ttk

import PIL.Image as pimg
import PIL.ImageTk as pimgtk

NAME = "MyAPP"

class Gui(tk.Tk): 
    '''Gui tkinter'''
    def __init__(self, parent):
        tk.Tk.__init__(self, parent)
        self.parent = parent

        self.dir = os.path.dirname(os.path.abspath(sys.argv[0]))
        self.icon = os.path.join(self.dir, 'img', 'Logo.xbm')
        self.img_logo = None
        self.logo = os.path.join(self.dir, 'img', 'Logo.png')
        
        self.initialize()

    def about(self):
        '''Displaying a window about'''
        from modules.window_about import gui

        init = {
            'DESCR': DESCR, 
            'dir': self.dir,
            'icon': self.icon,
            'img_logo': self.img_logo,
            'logo': self.logo,
            'NAME': NAME,
            }

        about = gui(None, init)
        about.title(NAME)
        about.mainloop()

    def initialize(self):
        '''Initialize window'''
        self.grid()
        (...)
        self.menu()
        self.ui()

    def menu(self):
        "Displaying menubar"
        menubar = tk.Menu(self)

        menuFile = tk.Menu(menubar, tearoff=0)
        menuFile.add_command(label="Créer", command=self.alert)
        menuFile.add_separator()
        menuFile.add_command(label="Quitter", underline=0,
                             command=self.byebye,
                             accelerator="Ctrl+Q")

        menubar.add_cascade(label="Fichier", underline=0, menu=menuFile)

        menuHelp = tk.Menu(menubar, tearoff=0)
        menuHelp.add_command(label="À-propos", underline=0,
                             command=self.about)

        menubar.add_cascade(label="Aide", underline=0, menu=menuHelp)

        self.config(menu=menubar)

    def ui(self):
        '''Displaying UI'''
        # Frame for service informations
        self.frm_service = ttk.Frame(self)
        self.frm_service.grid(row=7, column=3)

        ## Add an image logo
        logo_open = pimg.open(self.logo)
        self.img_logo = pimgtk.PhotoImage(logo_open)
        print(f'{self.img_logo}')
        img_service = ttk.Label(self.frm_service, image=self.img_logo)
        img_service.grid(row=0, column=0, rowspan=2)
        (...)
The img logo (self.img_logo) is correctly displaying into the gui.

But, into the script 'window_about.py', this code not run, with error:
Error:
pyimage2 self.DESCR: Yet Uploader Pixxie.py self.dir: ~/YUP.py self.icon: ~/YUP.py/img/Logo.xbm self.img_logo: pyimage2 self.logo: ~/YUP.py/img/Logo.png self.NAME: YUPixxie Exception in Tkinter callback Traceback (most recent call last): File "/usr/local/lib/python3.6/tkinter/__init__.py", line 1699, in __call__ return self.func(*args) File "~/YUP.py/yup.tk.py", line 431, in about about = gui(None, init) File "~/YUP.py/modules/window_about.py", line 33, in __init__ self.initialize() File "~/YUP.py/modules/window_about.py", line 40, in initialize self.ui() File "~/YUP.py/modules/window_about.py", line 64, in ui img_service = ttk.Label(frm_service, image=self.img_logo) File "/usr/local/lib/python3.6/tkinter/ttk.py", line 761, in __init__ Widget.__init__(self, master, "ttk::label", kw) File "/usr/local/lib/python3.6/tkinter/ttk.py", line 559, in __init__ tkinter.Widget.__init__(self, master, widgetname, kw=kw) File "/usr/local/lib/python3.6/tkinter/__init__.py", line 2293, in __init__ (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: image "pyimage2" doesn't exist
The python3 code into 'window_about.py':

import tkinter as tk
import tkinter.ttk as ttk

import PIL.Image as pimg
import PIL.ImageTk as pimgtk

class gui(tk.Tk):
    ''' Display a GUI for window about'''

    def __init__(self, parent, init):
        tk.Tk.__init__(self, parent)
        self.parent = parent

        # initialise self
        for i in init:
            setattr(self, i, init[i])
            print(f'self.{i}: {init[i]}')

        self.initialize()
    (...)

    def initialize(self):
        '''Initialize window'''
        self.grid()

        (...)
        self.ui()
        (...)

    def ui(self):
        '''Displaying UI'''
        # Frame for service informations
        frm_service = ttk.Frame(self)
        frm_service.grid(row=7, column=1)

        ## Add an image logo
        img_service = ttk.Label(frm_service, image=self.img_logo)
        img_service.grid(row=0, column=0, rowspan=2)
        (...)
why 'self.img_logo' - who is PhotoImage declaration - not run into 'window_about.py'.

I have the same error if i replace, into 'window_about.py', by this code:
        (...)
        logo_open = pimg.open(self.logo)
        img_logo = pimgtk.PhotoImage(logo_open)
        print(f'{img_logo}')
        img_service = ttk.Label(frm_service, image=img_logo)
        img_service.grid(row=0, column=0, rowspan=2)
        (...)
Reply
#2
is it pyimage2.png or some other suffix.
The error message is clear, it can't find the file.
Reply
#3
Are you really read my code?

First trying:
=> self.img_logo is initialized into class Gui() (in first script), here line 17 - same for self logo, and self.icon (line 16, 18)...
=> and is built into def ui(), in lines 74-76.
=> when you clic on menu about, the def about() is called; into this method, i code a dict named init - lines 26=>37. this is passed to def __init__() into script 'window_about.py'.
=> into the script 'window_about.py', the self.img_logo is called - line 37 - after builded into lines 15,16

'self.img_logo' is value after 'pimg.open()' of 'self.logo' and pimgtk.Photoimage()...

To be sure 'self.logo', 'self.icon' and 'self.img_logo' is builded, into script 'windows_about.py', i displaying thoses by print - line 17, as we see at the beginning of the error message:

Quote:self.DESCR: Yet Uploader Pixxie.py
self.dir: ~/YUP.py
self.icon: ~/YUP.py/img/Logo.xbm
self.img_logo: pyimage2
self.logo: ~/YUP.py/img/Logo.png
self.NAME: YUPixxie

Second trying:
=> i called only 'self.logo' into 'window_about.py', passed by init...
=> and build 'img_logo' after passing into 'pimg.open()' and 'pimgtk.PhotoImage()'...

Into 'window_about.py' script, if i code as:
## Add an image logo
        print(f'{self.logo}')
        img = pimg.open(self.logo)
        icon = pimgtk.PhotoImage(img)
        img_service = ttk.Label(frm_service, image=icon)
        img_service.grid(row=0, column=0, rowspan=2)
the result is:
Error:
~/YUP.py/img/Logo.png Exception in Tkinter callback Traceback (most recent call last): File "/usr/local/lib/python3.6/tkinter/__init__.py", line 1699, in __call__ return self.func(*args) File "~/YUP.py/yup.tk.py", line 436, in about about = gui(None, init) File "~/YUP.py/modules/window_about.py", line 34, in __init__ self.initialize() File "~/YUP.py/modules/window_about.py", line 41, in initialize self.ui() File "~/YUP.py/modules/window_about.py", line 68, in ui img_service = ttk.Label(frm_service, image=icon) File "/usr/local/lib/python3.6/tkinter/ttk.py", line 761, in __init__ Widget.__init__(self, master, "ttk::label", kw) File "/usr/local/lib/python3.6/tkinter/ttk.py", line 559, in __init__ tkinter.Widget.__init__(self, master, widgetname, kw=kw) File "/usr/local/lib/python3.6/tkinter/__init__.py", line 2293, in __init__ (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: image "pyimage3" doesn't exist
Do you want a screenshot for my directory image?

i repeat: 'self.logo' is same image, into same directory, on first script 'yup.tk.py' and on 'window_about.py' - this second script is call by the first!
and 'self.logo' is passed to script 'window_about.py'...
Reply
#4
Hi,

Nobody can help me to understand why a same code run correctly into main window, but not into called module?
Reply
#5
OK, i found a solution :

Into 'window_about.py', i used for the def __init__(), a tk.Toplevel, and after i can use the object 'self.img_logo':

class gui(tk.Tk):
    ''' Display a GUI for window about'''

    def __init__(self, parent, init):
        tk.Toplevel.__init__(self, parent)
        self.parent = parent

(...)
    def ui(self):
        '''Displaying UI'''
        # Frame for service informations
        frm_service = ttk.Frame(self)
        frm_service.grid(row=7, column=1)

        ## Add an image logo
        img_service = ttk.Label(frm_service, image=self.logo)
        img_service.grid(row=0, column=0, rowspan=2)
Problem resolved!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020