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_fet() or fet() 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.fet()
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.
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_fet() or fet() 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.fet()
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.
Share Improve this question asked Feb 14 at 2:35 Jack OvertonJack Overton 1 2 |1 Answer
Reset to default -2In 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.
The problem can be fixed.
The frm1Button1.pack()
call is blocking. That's the reason you can't observe the red frame.
- Move both
frm1Button1.pack()
prior tomainloop()
- Include
frame2.pack(fill=tk.BOTH, expand=True)
Script:
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.fet()
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)
frame2.pack(fill=tk.BOTH, expand=True)
frm2Button1=tk.Button(frame2,
text="Frame2 Button1",
bg="yellow",
fg="blue",
font=("Halvetica", 20))
frame1.bind("<Button-1>", lambda e: make_invisible(frm1Button1))
#frame2.bind("<Button-1>", lambda e: make_invisible(frm1Button1))
frm1Button1.pack()
frm2Button1.pack()
root.mainloop()
Screenshot:
frame2
is invisible because no layout manager is used on it. – acw1668 Commented Feb 14 at 3:43make_invisible
which perform different actions. – OldBoy Commented Feb 14 at 10:19