Skip to content Skip to sidebar Skip to footer

Tkinter.frame.grid Sizing Not Displaying Correctly

Hello I am making a table with a header for text to name the columns, cells to store data, and scrolling. The problem I'm having is that my Table is not displaying correctly. What

Solution 1:

Question: Frame Grid sizing not displaying correctly

You are misguided, the header_frameis not layouted using .grid(...). Using .create_window(... add the widget to the Canvas at the fixed position 0, 0, anchor='nw'. Therefore, no"Grid Layout Manager" magic, like auto resizing, happens.


Why do you get this Layout?Note: main_frame == 'blue', _headerCanvas == 'lightgreen', _cellCanvas == 'red':

ABCD Layout: A                             Layout: B                           Layout: C                             Layout: D

self._headerCanvas = tk.Canvas(main_frame, bg='lightgreen')
        self._headerCanvas.grid(row=0, column=0, pady=1, sticky='ew')

        main_frame.grid_rowconfigure(1, weight=1)

        self._cellCanvas = tk.Canvas(main_frame, bg='red')
        self._cellCanvas.grid(row=1, column=0, sticky='ew')

Layout: A: You create both Canvas with the default height. Therefore you get a similar layout using "The Grid Layout Manager".

Layout: B: Adding itmes to a Canvas, here the Cells Frame, does not change anything at the Layout.


Layout: C: Sync the Canvas height with the header_frame height results in.

  1. Using .bind('<Configure>'

    header_frame = tk.Frame(self._headerCanvas)
    header_frame.bind('<Configure>', self.on_configure)
    
    defon_configure(self, event):
        # Sync _headerCanvas height with header_frame height
        w = event.widget
        self._headerCanvas.configure(height=w.winfo_height())
    
  2. Using .update_idletask()

        header_frame = tk.Frame(self._headerCanvas)
        self._headers = Table.ColumnHeaders(header_frame, self._columnCount)
        root.update_idletasks()
        self._headerCanvas.configure(height=header_frame.winfo_height())
    

Still not what you want?Layout: D: You have allowed the .grid(row=1 to grow as much as possible by doing:

    main_frame.grid_rowconfigure(1, weight=1)

But you don't allow the Canvas to grow using sticky='ns'. This result in the 'blue' space above/bottom. Change to sticky='nsew'.

Post a Comment for "Tkinter.frame.grid Sizing Not Displaying Correctly"