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()```