最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Colors and frames not showing up in tkinter program - Stack Overflow

programmeradmin2浏览0评论

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
  • It is not clear what you mean by "the colors are also not working properly, only blue is showing up". Also frame2 is invisible because no layout manager is used on it. – acw1668 Commented Feb 14 at 3:43
  • You also have two functions named make_invisible which perform different actions. – OldBoy Commented Feb 14 at 10:19
Add a comment  | 

1 Answer 1

Reset to default -2

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.

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 to mainloop()
  • 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:

发布评论

评论列表(0)

  1. 暂无评论