I am using tkinter and trying to put some frames inside a grid of the mainframe. Inside those frames I want to put new widgets into new grids. But the problem is: tkinter thinks of the rows and columns of the "new" grids still as the ones from the old grid from the mainframe. How can I organize them in a nested grid?
from tkinter import *
from tkinter import ttk
root = Tk()
mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
upper_frame = ttk.Frame(mainframe).grid(row=0, column=0, sticky=(N, W))
lower_frame = ttk.Frame(mainframe).grid(row=1, column=0, sticky=(N, W))
label_1 = ttk.Label(upper_frame, text="Northwest ").grid(row = 0, column = 0)
label_2 = ttk.Label(upper_frame, text="Northeast ").grid(row = 0, column = 1)
label_3 = ttk.Label(lower_frame, text="Southwest ").grid(row = 0, column = 0)
label_4 = ttk.Label(lower_frame, text="Southeast ").grid(row = 0, column = 1)
root.mainloop()
CodePudding user response:
Note that the line:
upper_frame = ttk.Frame(mainframe).grid(row=0, column=0, sticky=(N, W))
has the same result as below:
x = ttk.Frame(mainframe)
upper_frame = x.grid(row=0, column=0, sticky=(N, W))
So upper_frame
is the result of .grid(...)
which is None
.
Using None
as the parent of a widget will make the widget the child of root window. So those labels are children of root window instead of the expected upper_frame
and lower_frame
.
You need to split the line:
upper_frame = ttk.Frame(mainframe).grid(row=0, column=0, sticky=(N, W))
into two lines:
upper_frame = ttk.Frame(mainframe)
upper_frame.grid(row=0, column=0, sticky=(N, W))
Same apply on lower_frame
.