1

I am relatively new to python (and tkinter), and I also seem to have difficulties with OOP. But I am trying :)

I found this on SO, and implemented it in my code:

I get some errors like: " TclError: image "pyimagexx" doesn't exist "

I think I've found that this is a garbage collecting issue? But I think I have followed the steps to circumvent that by original.load()?

And: - tk.Tk.configure(self, background="black")

Does not change the background color of the Frames. Where would I want to change the appearance of the Frames?

import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk

class myApp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.resizable(self, width=False, height=False)
        tk.Tk.configure(self, background="black")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (LoginPage, PageOne, PageTwo):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(LoginPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class LoginPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        player_name = tk.Label(self, text='Login Name').grid(row=1, column=1)
        player_name_entry = tk.Entry(self).grid(row=2, column=1)        
        player_pass = tk.Label(self, text='Password').grid(row=3, column=1)
        player_pass_entry = tk.Entry(self).grid(row=4, column=1)

        button = ttk.Button(self, text="Login", command=lambda: [controller.show_frame(PageOne)]).grid(row=5, column=1)


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        button1 = tk.Button(self, text="Logout",
                            borderwidth=0,
                            command=lambda: [controller.show_frame(LoginPage), self.logout()])     

        original = Image.open('Untitled.gif')
        ph_im = ImageTk.PhotoImage(original)
        button1.config(image=ph_im)
        original.load()
        button1.grid(row=2, column=1)


        button2 = tk.Button(self, text="PageTwo",
                            command=lambda: [controller.show_frame(PageTwo)])
        button2.grid(row=2, column=2)

    def logout(self):
        print("Logged out")

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        button1 = tk.Button(self, text="Logout",
                             borderwidth=0,
                             command=lambda: [controller.show_frame(LoginPage), self.logout()])
        button1.grid(row=2, column=1) 

        button2 = tk.Button(self, text="PageOne",
                             command=lambda: [controller.show_frame(PageOne)])
        button2.grid(row=2, column=2)

    def logout(self):
        print("Logged out")

app = myApp()
app.geometry("1500x750")
app.mainloop()```
  • 1
    every frame has own color - and it is not transparent color - and you change color only in main window `Tk()`- but not in frames. So black color can be hidden behind Frames. In `Pages` you may need `self.configure(background="black")` – furas Aug 14 '19 at 13:27
  • in `PageOne` use `button1.image = ph_im` and then GC will not remove image from memory. And you will see image. OR use `self.ph_im` instead of `ph_im` in all code. – furas Aug 14 '19 at 13:35

1 Answers1

0

Try the next code to check your path. I thing that your relative path is not ok. If /image.gif don't works maybe your path is in other folder and you need to do the correct relative path. folder_example/folder_example/image.gif (Windows)

import os
print(os.system('dir'))

(Debian)

import os
print(os.system('ls'))
Pablo Díaz
  • 330
  • 4
  • 14