Search code examples
python-3.xtkintergridexpand

tkinker grid columconfigure and row configure not working


Hi I need help with the row configure and columconfigure. The program is build from tk, which the frame inherits from, then the notebook(tabs), which inherits from frame, then grid inheriting from the notebook. Up to the notebook, everything is expanding the way that I wanted.

My problem is the grid system is not expanding. I put the row and column configure with weight inside and it's not working. 1). I don't know if I reference it right

self.tabControl.columnconfigure(employee, weight = 1)

2). Since I have pack() for the first 2 levels, do I have to do the same column and row configure to root? if I need where and how would I put it?

here is the program:

import tkinter as tk
from tkinter import ttk

#upper tabs
upper_tabs = ["Final", "Requests"]
tabs = {}

Employees = ["A", "B", "C", "D", "E", "F", "G", ]
Days= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",   
"Saturday", "Sunday"]

class Application(ttk.Frame): #inherent from frame.

    def __init__(self, parent):
        tk.Frame.__init__(self, parent, bg="ivory2", bd=2,       
          relief=tk.RAISED )
        self.parent = parent
        self.pack(fill=tk.BOTH, expand=1)

        self.GUI()

    def GUI(self): #the function that runs all the GUI functions.

        self.create_tabs()
        self.buttons()

        for name in upper_tabs:
            self.create_grid(name)

####---------create grid_GUI---------------------####

    def create_tabs(self):
        self.tabControl = ttk.Notebook(self, width="1100", height= 
        "500") #Create Tab Control

        for name in upper_tabs:
            self.tab=ttk.Frame(self.tabControl)# Create a tab
            self.tabControl.add(self.tab, text=name)      # Add the tab
            tabs[name] = self.tab
            self.tabControl.pack(expand=1, fill=tk.BOTH)  # Pack to      
            make visible

    def create_grid(self, name):

        for employee in range(len(Employees)+2):
            self.tabControl.columnconfigure(employee, weight = 1)
            for day in range(len(Days)+2):
                self.tabControl.rowconfigure( day, weight = 1)
                self.label = tk.Label(tabs[name], relief="ridge",    
                width=12, height=3)
                self.label.grid(row=employee, column=day, )



    def buttons(self):

        self.button=tk.Button(self, text="Caluculate", bg="salmon", )
        self.button.pack(side= "right")

def main():

    root = tk.Tk()
    root.title("class basic window")
    root.geometry("1200x600")
    root.config(background="LightBlue4")
    app = Application(root)
    root.mainloop()

if __name__ == '__main__':
    main()

thanks for your help.


Solution

  • You are setting an explicit size for the labels and to have them change size with the tab you have to specify sticky='nsew'.

    Then you row- and columnconfigure tabControl when you should do it to each of the tabs.

    I made changes accordingly and added padding to the notebook and button to make the layout a bit more sparse. The notebook will now expand with the window. I set a minimum height in the rowconfigure of the Application frame so the button will not disappear when the window shrinks.

    I changed all geometry to grid() because it's easier when the layout becomes more complex.

    I have commented the significant changes in the code below:

    import tkinter as tk
    from tkinter import ttk
    
    #upper tabs
    upper_tabs = ["Final", "Requests"]
    tabs = {}
    
    Employees = ["A", "B", "C", "D", "E", "F", "G", ]
    Days= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
           "Saturday", "Sunday"]
    
    class Application(tk.Frame): #inherent from frame.
        def __init__(self, parent):
            tk.Frame.__init__(self, parent, bg="tan")
            self.parent = parent
            self.pack(fill=tk.BOTH, expand=1)
            # Configure self to fill parent when size changes
            self.rowconfigure([0,1],weight=1, minsize=40)
                # Minsize will keep row=1 at least 50 pixels high or the
                # button will disappear when the frame shrinks
            self.columnconfigure([0,1],weight=1)
            self.GUI()
    
        def GUI(self): #the function that runs all the GUI functions.
            self.create_tabs()
            self.buttons()
            for name in upper_tabs:
                self.create_grid(name)
    
    ####---------create grid_GUI---------------------####
    
        def create_tabs(self):
            self.tabControl = ttk.Notebook(self, width=1100, height=500)
            for name in upper_tabs:
                self.tab=tk.Frame(self.tabControl, bg='thistle')
                self.tabControl.add(self.tab, text=name)
                tabs[name] = self.tab
                # Set sticky to fill up available space and padding for layout
                self.tabControl.grid(row=0, column=0, padx=10, pady=10, sticky='nsew')  
    
        def create_grid(self, name):
            for employee in range(len(Employees)+2):
                # columnconfigure tab instead of tabControl
                tabs[name].columnconfigure(employee, weight = 1)
                for day in range(len(Days)+2):
                    # rowconfigure tab instead of tabControl
                    tabs[name].rowconfigure(day, weight=1)
                    self.label = tk.Label(tabs[name], relief="ridge", width=12, height=3)
                    # Set sticky to fill available space
                    self.label.grid(row=employee, column=day, sticky='nsew')
    
        def buttons(self):
            self.button=tk.Button(self, text="Caluculate", bg="salmon", )
            # Adding padding for layout
            self.button.grid(row=1, column=0, padx=(0,10), pady=(0,10), sticky='e')
    
    def main():
        root = tk.Tk()
        root.title("class basic window")
        root.config(background="LightBlue4")
        app = Application(root)
        root.mainloop()
    
    if __name__ == '__main__':
        main()
    

    Is this what you are aiming for?