1

I'm having an issue where nothing is being placed in the frame and I'm not sure why as the template works when I tested that and can't see where I have done anything different other than use different widgets

Template used: https://www.semicolonworld.com/question/42826/switch-between-two-frames-in-tkinter

This is my code with most of the items removed other than a couple of examples

import tkinter as tk
from tkinter import *

class App(tk.Tk):

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

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=False)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            #frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

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

        def check_user_login():
            #code removed
            return None

        #create a canvas to place images on
        canvas = tk.Canvas(self, bg = "#5c5c5c",height = 400,width = 800,bd = 0,highlightthickness = 0,relief = "flat")
        canvas.place(x = 0, y = 0)


        #login button
        button = tk.Button(self, text="Go to the start page",command=lambda: controller.show_frame("PageOne"))
        button.place(x = 356, y = 290,width = 89,height = 29)



class PageOne(tk.Frame):

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

        #create a canvas to place images on
        canvas = Canvas(self,bg = "#5c5c5c",height = 400,width = 800,bd = 0,highlightthickness = 0,relief = "flat")
        canvas.place(x = 0, y = 0)
        
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


if __name__ == "__main__":
    app = App()
    app.geometry("800x400")
#    app.configure(bg = "#5c5c5c")
    app.resizable(False, False)
    app.mainloop()

Any help would be massively appreciated I've been stuck on this for a while

Will Jordan
  • 245
  • 2
  • 12
  • Please try to make a [mcve] that doesn't require images, unless the problem can only be reproduced with the image. – Bryan Oakley Sep 02 '21 at 02:03
  • oh sorry, I'll try and edit it now @BryanOakley – Will Jordan Sep 02 '21 at 02:04
  • @BryanOakley I think I have made it without images, ofc the same problem is there, is the problem using a canvas inside a frame? – Will Jordan Sep 02 '21 at 02:11
  • hint: the widgets are placed in the frame. It's the frame that isn't visible. – Bryan Oakley Sep 02 '21 at 02:12
  • @BryanOakley well that sounds promising at least, although I can't see why the frame would not be visible as I've looked at the template which works fine and the only difference I can see is the use of canvas/place which you are suggesting has nothing to do with the problem. I'm sorry I'm not able to find where the issue is :( – Will Jordan Sep 02 '21 at 02:24
  • @BryanOakley I just changed place to pack and yes it does seem that the frame is just not visible like you said but I've still no idea as to why. Do you know the answer? – Will Jordan Sep 02 '21 at 02:31
  • You've commented out `#frame.grid(row=0, column=0, sticky="nsew")` – Bryan Oakley Sep 02 '21 at 02:39
  • @BryanOakley I've made a few changes since you pointed out the line I commented out such as using .grid for placing the canvas. But ultimately you have helped me out massively there so thank you so much, it is working now – Will Jordan Sep 02 '21 at 02:58

0 Answers0