1
votes

I have a problem wherein I cannot create multiple distinct frames within a given frame. This is just a small chunk of my overall application but basically I've separated different Frames into classes and i call those classes depending on the situation.

What I am looking for is how to create 2 or more frames within this frame class. As of right now, the distinct frames aren't being created so when place them in a grid they overlap and replace each other. (i.e. frame2.label overlaps with frame1.label)

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

        #First frame inside the current MainPage Window Frame
        frame1 = Frame(self, bg = "red")
        frame1.grid(row = 0, column = 0, sticky = W)

        frame1.label = tk.Label(self, text="Main Page", font=SMALL_FONT)
        frame1.label.grid(row = 0, column = 0, sticky = W)

        itemScanNumber = StringVar()    
        frame1.EntryBox = ttk.Entry(self, textvariable = itemScanNumber) 
        frame1.EntryBox.grid(row = 0, column = 1, sticky = W)

        print(itemScanNumber.get())

        def printNumber():
            print("Your Number: " + itemScanNumber.get())
            return

        frame1.button = ttk.Button(self, text = "CLICK ME FOR SCAN NUMBER!", command = printNumber)
        frame1.button.grid(row = 0, column = 2, sticky = W)

        frame1.button1 = ttk.Button(self, text = "CLICK ME!", command = lambda: controller.show_frame(MasterFilePage))
        frame1.button1.grid(row = 0, column = 3, sticky = W)


        #Second frame inside the current MainPage Window Frame
        frame2 = Frame(self, bg = "blue")
        frame2.grid(row = 1, column = 0, sticky = W)

        frame2.label = tk.Label(self, text="Main Page2", font=SMALL_FONT)
        frame2.label.grid(row = 0, column = 0, sticky = W)
1
You are putting both frame1 and frame1.label in the main frame, and they are both in row 0, column 0. What are you trying to achieve with that, or did you intend to put frame1.label inside frame1? - Bryan Oakley
Yes my intention was to create frame1 and add a label to frame1. I assume that this is not the way you're supposed to do this? - Miggy
It's almost never a good idea to put two widgets in the same row and column, except in very specific use cases. - Bryan Oakley
Well my intention was to create two frames and have separate widgets within those frames. When I run my program, it seems like the 2 frames: frame1 and frame2 aren't being created or aren't properly identified, hence why frame1.label and frame2.label overlap. Or at least that's what I think is going on. I want 2 distinct frames inside this frame/window class is because I would like to eventually add a canvas or another widget that is bigger than the allotted space in the rows/columns of frame1. - Miggy
You seem to have an inconsistent use of import. You're both importing tkinter ("as tk", apparently), and doing a wildcard import. Do just one or the other, preferably the former. - Bryan Oakley

1 Answers

1
votes

You are successfully putting two frames inside of the MainPage frame. The problem is that you're putting a bunch of other widgets in there too, and you're putting some of them in the same row and column as the frames.

For the widgets to be inside the frames, you need to provide the frame as the parent. For example:

    frame1.label = tk.Label(frame1, ...) 
    frame1.EntryBox = ttk.Entry(frame1, ...) 
    frame1.button = ttk.Button(frame1, ...)
    frame1.button1 = ttk.Button(frame1, ...)
    frame2.label = tk.Label(frame2, ...)