I've been trying for days to figure out how to just put grids in grids of objects. I've got two frames (which I guess are widgets in Tk?) I add one to the other, but the position of its widgets don't appear to respect the parent widgets (and it just overwrites them).
here is my MCVE
import tkinter as tk
class TestDoubleNested(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.grid()
self.test_label = tk.Label(text="AAAAAAAAA")
self.test_label.grid(row=0, column=0)
class TestNested(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.grid()
self.test_label = tk.Label(text="top_level_test_label")
self.test_label.grid(row=0, column=0)
self.test_label2 = tk.Label(text="top_level_test_label2")
self.test_label2.grid(row=0, column=1)
# expected to be in 3rd column...
self.test = TestDoubleNested(master)
self.test.grid(row=0, column=4)
test = TestNested()
test.master.title("Test Example")
test.master.maxsize(1000, 400)
test.master.wm_geometry("400x300")
test.mainloop()
No matter how I move around self.grid() invokations or change the column stuff, the display is the same:
As you can see AAAAAAAA displays on top of another widget from the parent, where ideally it should display to the side of everything.
You aren't specifying the master
for each widget, so they are all being added to the root window. If you want widgets to be inside frames, you must specify the frame as the first argument when creating the widgets:
class TestDoubleNested(tk.Frame):
def __init__(self, master=None):
...
self.test_label = tk.Label(self, text="AAAAAAAAA")
...
class TestNested(tk.Frame):
def __init__(self, master=None):
...
self.test_label = tk.Label(self, ...)
self.test_label2 = tk.Label(self, ...)
self.test = TestDoubleNested(self)
...