1
votes

I'm trying to equally distribute three objects/widgets across one row in a ttk notebook tab, but the three objects only expand half of the window.

I'm not sure what controls the number of columns within a tab since columnsspan in tab.grid(row=0, columnspan=3) doesn't appear to change anything. I've also tried various values for rows and columns for every object with .grid. This is only an issue for notebook tabs, rather than a single window.

#!/usr/bin/env python3

from tkinter import ttk
from tkinter import *

root = Tk()
root.title('Title')
root.resizable(width=FALSE, height=FALSE)
root.geometry('{}x{}'.format(750, 750))

nb = ttk.Notebook(root)
nb.grid(row=0, column=0)

# Add first tab
tab1 = ttk.Frame(nb)
#tab1.grid(row=0, column=0)
nb.add(tab1, text='Setup')

# Add row label
lb1 = ttk.Label(tab1, text = 'Parent Directory:')
lb1.grid(row = 1, column = 1)

# Add text entry
txt1 = ttk.Entry(tab1)
txt1.grid(row = 1, column = 2)

# Add selection button
btn1 = ttk.Button(tab1, text="Select")
btn1.grid(row=1, column=3)

root.mainloop()

I'm expecting the columns to span the full length of the window, instead of half the length of the window.

enter image description here

1

1 Answers

2
votes

In order to do this using grid you need to use the Frame.columnconfigure([column#], minsize=[minsize]) function.

If you want the text box and button to stretch to fill the space, use the sticky option. (Sticky doesn't really do anything with the label)

Code:

#!/usr/bin/env python3

from tkinter import ttk
from tkinter import *

root = Tk()
root.title('Title')
root.resizable(width=FALSE, height=FALSE)
root.geometry('{}x{}'.format(750, 750))

nb = ttk.Notebook(root, width=750)
nb.grid(row=0, column=0)

# Add first tab
tab1 = ttk.Frame(nb)
#tab1.grid(row=0, column=0)
nb.add(tab1, text='Setup')

# Change the sizes of the columns equally
tab1.columnconfigure(1, minsize=250)
tab1.columnconfigure(2, minsize=250)
tab1.columnconfigure(3, minsize=250)

# Add row label
lb1 = ttk.Label(tab1, text = 'Parent Directory:')
lb1.grid(row = 1, column = 1,sticky=(E,W))

# Add text entry
txt1 = ttk.Entry(tab1)
txt1.grid(row = 1, column = 2,sticky=(E,W))

# Add selection button
btn1 = ttk.Button(tab1, text="Select")
btn1.grid(row=1, column=3,sticky=(E,W))

root.mainloop()

Image of result