Search code examples
pythontkinterhtml-tablecustomtkinter

CTkFrame rowspan is functioning incorrectly and differently from normal tk.Frame


I have several frames, each of which is configured to have 1 column and 30 rows. I want to display several frames within them, each spanning various amounts of rows via rowspan. However, when I set the rowspan=2 for the CTkFrame, it expands the rows it occupies, despite these rows being marked with 'uniform='a'' when initially set. What confuses me, is that when I replace the CTkFrame with a normal tk.Frame, it functions just fine. On the attached screenshot the CTkFrame is in the second column, while the tk.Frame is in the third.

Here is the code that generates this (specifically the class that governs the columns):

import tkinter as tk
import customtkinter as ctk
from Settings import *

class ScheduleFrame(ctk.CTkFrame):
    def __init__(self, parent):
        ctk.CTkFrame.__init__(self,parent, fg_color='transparent')
        
        frameContainer = ctk.CTkFrame(self, fg_color=c_bg)
        frameContainer.pack(expand=True, fill='both')
        frameContainer.rowconfigure(0, weight=1, uniform='a')
        numberOfRows = 30
        for c in range(8):
            frameContainer.columnconfigure(c,weight=1, uniform='a')
        
        self.columns = []
        for c in range(8):
            col = ctk.CTkFrame(frameContainer, corner_radius=5, fg_color=c_panel)
            col.grid(row=0, column = c, sticky='nsew', padx=2, pady=2)
            col.columnconfigure(0, weight = 1, uniform='a')

            for i in range(numberOfRows):
                col.rowconfigure(i, weight = 1, uniform = 'a')
            self.columns.append(col)

        # Normal tk.Frame
        topFrame = tk.Frame(self.columns[1])
        topFrame.grid(column=0, row = 0, sticky='nsew', rowspan=2, padx=1, pady=1)
        
        # Weird CTkFrame
        topFrame2 = ctk.CTkFrame(self.columns[2])
        topFrame2.grid(column=0, row = 0, sticky='nsew', rowspan=2, padx=1, pady=1)

        # labels with numbers for debug
        for i, c in enumerate(self.columns):
            for r in range(numberOfRows):
                # normal cells
                if r > 1:
                    label = ctk.CTkLabel(c, text=r, corner_radius=2,fg_color=c_panel_l)
                    label.grid(column=0, row = r, sticky='nsew', padx=1, pady=1)


Here is what the result looks like: Resulting layout My question is — what am I doing wrong and how can I make sure that the CTkFrame does not expand the rows it occupies?

P.S. I have tried changing the value of rowspan for the CTkFrame, which did not fix the issue. P.S.S. I have tried to move the CTkFrame to a different row, which did not help either.


Solution

  • I have found what I believe to be a workaround, posting it in case someone else encounters a similar issue: When creating CTkFrame I set a height smaller than the space it is supposed to occupy, then position it via .grid method. The resulting code bit is:

    topFrame2 = ctk.CTkFrame(self.columns[2],height=5)
        topFrame2.grid(column=0, row = 0, sticky='nsew', rowspan=2, padx=1, pady=1)
    

    As a result the CTkFrame is occupies the same space as the tk.Frame. P.S. This seems not to be a workaround, but an intended solution, according to acw1668, as the default height of the CTkFrame is 400 and is not overridden by the .grid & sticky='nsew' positioning.