Python Forum
Column headers not aligning properly with table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Column headers not aligning properly with table
#4
I've worked on this awhile and can't seem to get it align right and the scrollbar to work at the same time. Widths are right if you uncomment the scrollframe grid. But the scrollbar doesn't work. Maybe you can find a work around.

#! /usr/bin/env python3
import tkinter as tk
from tkinter import ttk

class MyClass:
    def __init__(self, parent):
        self.parent = parent
        self.parent.columnconfigure(0, weight=1)
        self.parent.rowconfigure(0, weight=1)

        self.mainframe = tk.Frame(self.parent)
        self.mainframe.grid(column=0, row=0, sticky='new')
        self.mainframe.grid_columnconfigure(0, weight=3)

        self.headerframe = tk.Frame(self.mainframe)
        self.headerframe.grid(column=0, row=0, sticky='ew')
        for i in range(0, 8):
            self.headerframe.grid_columnconfigure(i, weight=3, uniform='header')


        self.contentframe = tk.Frame(self.mainframe, bg='green')
        self.contentframe.grid(column=0, row=1, sticky='new')
        self.contentframe['relief'] = 'solid'
        self.contentframe['borderwidth'] = 1
        self.contentframe['highlightbackground'] = 'grey65'
        self.contentframe['highlightthickness'] = 1
        self.contentframe.grid_columnconfigure(0, weight=3)


        canvas = tk.Canvas(self.contentframe, bg='pink')
        scrollbar = ttk.Scrollbar(self.contentframe, orient='vertical', command=canvas.yview)
        self.scrollframe = tk.Frame(canvas)


        self.scrollframe.bind(
            '<Configure>',
            lambda event: canvas.configure(scrollregion=canvas.bbox('all'))
        )

        canvas.create_window((0, 0), window=self.scrollframe)
        canvas.configure(yscrollcommand=scrollbar.set)

        # You can comment this out and the scroll fame will work but, it shrinks
        self.scrollframe.grid(column=0, row=0, sticky='news')

        for i in range(0, 8):
            self.scrollframe.grid_columnconfigure(i, weight=3, uniform='test')

        scrollbar.grid(column=1, row=0, sticky='ns')
        canvas.grid(column=0, row=0, sticky='news')
        canvas.grid_columnconfigure(0, weight=3)



        self.headers()
        self.combo_box(40)
        self.entrybox(1, 20, 5)
        self.chkbox(3, 30)
        self.entrybox(6,10, 15)
        self.chkbox(7, 10)





    def headers(self):
        headers = ["Field Name","Field Label","Default Value","ReadOnly","Copy","Mandatory","Sort Order","Remove"]
        col = 0
        for header in headers:
            label = tk.Label(self.headerframe)
            label['text'] = header
            label['relief'] = 'solid'
            label['borderwidth'] = 1
            label['highlightbackground'] = 'grey65'
            label['highlightthickness'] = 1
            label.grid(column = col, row=0, sticky='ew')
            col += 1

    def combo_box(self, howmany):
        for i in range(0, howmany):
            combobox = ttk.Combobox(self.scrollframe)
            combobox.grid(column=0, row=i, sticky='new', padx=1)

    def entrybox(self, col, howmany, padx):
        row = 0
        col = col
        for i in range(0, howmany):
            entry = tk.Entry(self.scrollframe)
            entry.grid(column=col, row=row, sticky='new', padx=padx)
            if row >= 9:
                row = 0
                col += 1
            else:
                row += 1


    def chkbox(self, col, howmany):
        row = 0
        col = col

        for i in range(0, howmany):
            box = tk.Checkbutton(self.scrollframe)
            box.grid(column=col, row=row)
            if row >= 9:
                row = 0
                col += 1
            else:
                 row += 1




def main():
    root = tk.Tk()
    root['padx'] = 10
    root['pady'] = 5
    root['relief'] = 'ridge'
    root['borderwidth'] = 5
    MyClass(root)
    root.mainloop()

if __name__ =='__main__':
    main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
RE: Column headers not aligning properly with table - by menator01 - Jul-13-2020, 12:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Aligning Python Tkinter Buttons In Rows TollyThaWally 3 8,131 Feb-22-2018, 02:56 AM
Last Post: woooee
  which column of sql table is equal to a variable gray 7 6,203 Mar-20-2017, 04:11 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020