0

The following is my code. After I run it, I find that my button and entry are not displayed. I don't know what went wrong.

import tkinter as tk
import ttkbootstrap as ttk
root = tk.Tk()
root.title("test")
root.geometry("1200x740")
target_group = ttk.LabelFrame(root, text="test", bootstyle="info")
target_group.grid(row=0, column=0, padx=10, pady=10)
a_label = ttk.Label(target_group,text="test",bootstyle="inverse-danger",font=("Times New Roman", 15))
a_label.grid(row=0, column=0, padx=20, pady=5)
a = tk.StringVar(target_group, value="test")
a_text = ttk.Entry(target_group, bootstyle="success", width=91, textvariable=a)
a_text.grid(row=0, column=1, padx=5, pady=5)
e_group = ttk.LabelFrame(root, text="test", bootstyle="info")
e_group.grid(row=1, column=0, padx=10, pady=10)
o = ttk.Frame(e_group)
c_frame = ttk.Frame(e_group)
e_notebook = ttk.Notebook(e_group, bootstyle="info")
e_notebook.add(o, text='test1')
e_notebook.add(c_frame, text='test2')
def collect_i():
    pass
i_collect_button = tk.Button(o, text="Start Collect", command=collect_i, width=30)
i_collect_button.grid(row=0, column=0, padx=5, pady=5)
i_show = tk.StringVar(o, value="test")
i_show_text = tk.Entry(o, width=61, textvariable=i_show)
i_show_text.grid(row=1, column=0, padx=5, pady=5)
e_notebook.grid(row=0, column=0, padx=10, pady=10)
root.mainloop()

test

After I tried it, I found that if I replace i_collect_button = tk.Button(o, text="Start Collect~", command=collect_i, width=30) with i_collect_button = tk.Button(target_group, text="Start Collect~", command=collect_i, width=30), it can run successfully.

w01f
  • 1
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Aug 13 '23 at 12:52

1 Answers1

1

The button and entry aren’t visible because they are hidden behind the notebook. The stacking order (z-order) of widgets is dependent on the order that they are created, and through the widget hierarchy.

You're creating the notebook frames before the notebook, and as children of a higher level frame than the notebook. Therefore they are lower in the stacking order than the notebook which will sit on top of these other widgets even though you add them later as tabs in the notebook.

You can see this by adding o.lift() after creating the notebook to raise it above the notebook in the stacking order (though this is not the right fix) :

e_notebook = ttk.Notebook(e_group, bootstyle="info")
o.lift()

The better solution is to make the frame for each tab be a child of the notebook rather than a child of the containing frame:

e_notebook = ttk.Notebook(e_group, bootstyle="info")
o = tk.Frame(e_notebook, background="pink")
c_frame = ttk.Frame(e_notebook)
e_notebook.add(o, text='test1')
e_notebook.add(c_frame, text='test2')
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685