Home > Net >  Missing Frame/Not Appearing (Tkinter, Python)
Missing Frame/Not Appearing (Tkinter, Python)

Time:02-04

I'm trying to create a GUI and was using Tkinter to create that GUI, I had one root with three different frames, one on the top, on the bottom left and one on the bottom right. The top hasn't been appearing and i was wondering if i did something wrong and how i could fix it. (I'm also very new to Tkinter and Python)

import tkinter as tk
from typing import Text
MainRoot= tk.Tk()
MainRoot.wm_geometry("800x500")
MainRoot.wm_title("Encode")



Encode_Frame = tk.Frame(MainRoot, bg="Gray", height=400, width=400)
Encode_Frame.pack(anchor="s",side=tk.LEFT)

Decode_Frame = tk.Frame(MainRoot, bg="Blue", height= 400, width=400)
Decode_Frame.pack(anchor="s",side=tk.RIGHT)

Text_Frame= tk.Frame(MainRoot,bg="Red",height=100,width=800)
Text_Frame.pack(side=tk.TOP, anchor= "n")



MainRoot.mainloop()

CodePudding user response:

The root cause is that you're packing the left and right sides first. The way the packer works, when you pack a widget it takes up an entire side. So, once you pack something on the left, nothing can appear above or below it. Likewise when you pack something on the right.

When you pack the left and right first, the only space left is in the middle between the two sides. So, your third frame is going to the top _of the space between the two sides. Since the two sides are already filling the entire width of the window, there's no room left for the top frame.

The simple solution is to pack the top frame first.

Text_Frame.pack(side=tk.TOP, anchor= "n")
Encode_Frame.pack(anchor="s",side=tk.LEFT)
Decode_Frame.pack(anchor="s",side=tk.RIGHT)

For a comprehensive explanation of pack with images, see grid layout; grid lines in red

In this example, grid lines are in red. The "preview" spans three rows.

The following code created this layout:

def create_widgets(root, w):
    """Create the window and its widgets.

    Arguments:
        root: the root window.
        w: SimpleNamespace where widgets can be stored.
    """
    # General commands and bindings
    root.wm_title("Tkinter Color v"   __version__)
    root.columnconfigure(1, weight=1)
    root.rowconfigure(0, weight=1)
    root.rowconfigure(1, weight=1)
    root.rowconfigure(2, weight=1)
    # First row
    tk.Label(root, text="Red").grid(row=0, column=0, sticky="w")
    w.red = tk.Scale(
        root, from_=0, to=255, orient=tk.HORIZONTAL, length=255, command=do_red
    )
    w.red.grid(row=0, column=1, sticky="nsew")
    w.lf = tk.LabelFrame(root, text="preview")
    w.lf.grid(row=0, column=2, rowspan=3)
    w.show = tk.Frame(w.lf, width=100, height=100, bg="#000000")
    w.show.pack(padx=2, pady=2)
    # Second row
    tk.Label(root, text="Green").grid(row=1, column=0, sticky="w")
    w.green = tk.Scale(
        root, from_=0, to=255, orient=tk.HORIZONTAL, length=255, command=do_green
    )
    w.green.grid(row=1, column=1, sticky="ew")
    # Third row
    tk.Label(root, text="Blue").grid(row=2, column=0, sticky="w")
    w.blue = tk.Scale(
        root, from_=0, to=255, orient=tk.HORIZONTAL, length=255, command=do_blue
    )
    w.blue.grid(row=2, column=1, sticky="ew")
    # Last row
    w.b = tk.Button(root, text="Quit", command=do_exit)
    w.b.grid(row=3, column=0, sticky="w")
``
  •  Tags:  
  • Related