[Python] Colors and frames not showing up in tkinter program

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    In a course I am taking, I was following along with the code my professor was writing, but when I run it there is not a second frame showing up and the colors are also not working properly, only blue is showing up.

    """
    practice: use of pack_forget() or forget() with two frames
    """

    import tkinter as tk

    root = tk.Tk()
    root.geometry("400x300+500+200")

    frame1=tk.Frame(root, bg="red", width=200, height=200)
    frame1.pack_propagate(0)
    frame1.pack(fill=tk.BOTH, expand=True)


    def make_invisible(widget):
    widget.forget()

    frm1Button1=tk.Button(frame1,
    text="Frame1 Button1",
    bg="yellow",
    fg="blue",
    font=("Halvetica", 20))
    frame1.bind("<Button-1>", lambda e: make_invisible(frm1Button1))
    frm1Button1.pack(fill=tk.BOTH, expand=True)

    def make_invisible(widget):
    widget.pack(fill=tk.BOTH, expand=True)

    frame2=tk.Frame(root, bg="blue")
    frame2.pack_propagate(0)
    frm2Button1=tk.Button(frame2,
    text="Frame2 Button1",
    bg="yellow",
    fg="blue",
    font=("Halvetica", 20))
    frm2Button1.pack(fill=tk.BOTH, expand=True)

    root.mainloop()


    The code was expanded upon further but I did not get to that part because I was trying to fix the problem, so if it looks messy or obviously wrong, that's why.

    I have asked copilot if they had any fixes but it just led to the python window looking even more messy. I am pretty new to tkinter and python in general so bear with me lol.

    Continue reading...

Compartilhe esta Página