[Tkinter] image doesnt exists into personal module - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] image doesnt exists into personal module (/thread-8680.html) |
image doesnt exists into personal module - PyForIO - Mar-03-2018 Hi all... I create an interface tkinter, with python3. (3.6.2 on OpenBSD) My folder: Quote:yup.tk.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: 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) (...) RE: image doesnt exists into personal module - Larz60+ - Mar-03-2018 is it pyimage2.png or some other suffix. The error message is clear, it can't find the file. RE: image doesnt exists into personal module - PyForIO - Mar-04-2018 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 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: 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'... RE: image doesnt exists into personal module - PyForIO - Mar-07-2018 Hi, Nobody can help me to understand why a same code run correctly into main window, but not into called module? RE: image doesnt exists into personal module - PyForIO - Mar-13-2018 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! |