29

Consider this simple code:

from Tkinter import *
import ttk
root= Tk()
ttk.Label(root, text='Heading Here').grid(row=1, column=1)
ttk.Separator(root,orient=HORIZONTAL).grid(row=2, columnspan=5)
root.mainloop()

When I run this code, the separator is almost invisible.
ttk separator not visible
I have marked it with a red arrow, if you can see it as a small dot kind of thing.

How do I make the separator span the entire horizontal width, or at least be visible?

Cœur
  • 37,241
  • 25
  • 195
  • 267
bhaskarc
  • 9,269
  • 10
  • 65
  • 86
  • Anyone who stumble upon this, the easier solution for me to use `Label` like this: `Label(root, text=' | ').grid(row=.., column=.., sticky="w")` – sagarr Aug 23 '18 at 10:30

3 Answers3

45

The separator has a natural width of 1 pixel. You told it to reserve the space across five columns, but you haven't requested that the separator actually fill those five columns. To solve this, supply the sticky attribute, which says "if there's more space than needed for this widget, make the edges of the widget "stick" to specific sides of its container".

In this case, you want the separator to sticky to the left and right edges of it's container. The sticky attributes uses the points of the compass for values, so you want "e" for east, and "w" for west:

ttk.Separator(...).grid(..., sticky="ew")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
8

@Bryan's sticky solution is fine but it only solves part of the case, since the separator will not cover the entire horizontal width (that you asked). Here is an alternative solution that you can apply: Instead of .grid(), use place() layout and have full control of the position, the width (length) and even the height of the separator. To just apply this to the present case (w/o any extra feature) and cover the full horizontal width, just:

Replace

ttk.Separator(root,orient=HORIZONTAL).grid(row=2, columnspan=5)

with

ttk.Separator(root).place(x=0, y=26, relwidth=1)

You can set 'y' as you like. Also note that orient=HORIZONTAL is not needed since it's the default option. (Check http://effbot.org/tkinterbook/place.htm for details and examples of the use of `.option()' layout.)

Apostolos
  • 3,115
  • 25
  • 28
  • 2
    I think hard-coded layouts show a lack of abstraction and are inelegant. Also they're hard to change, don't shift when you change content size, or move when users resize the window. – nyanpasu64 May 24 '18 at 02:31
  • You are right in general, but not in this case. Moving, resizing or whatever other change won't affect the separator. On the other hand, the above solution handles the case. – Apostolos May 25 '18 at 07:03
4

You probably have to give the Separator an ipadx so that it will be visible. In your case it is visible but you can't see it because of it's width. Try this:

Separator(root, orient=HORIZONTAL).grid(row=1,column=0,columnspan=4, ipadx=100) 
David Buck
  • 3,752
  • 35
  • 31
  • 35
crispengari
  • 7,901
  • 7
  • 45
  • 53