3
votes

How to set/change the length/width of a ttk.Separator object in Tkinter?

        ttk.Separator(self, orient='horizontal').grid(column=0,
        row=0, columnspan=2, sticky='ew')

It seems that columnspan tries to do the job, but when you have multiple separators with the same columnspan, they appear to have different lengths - any idea why?

Here is a simple quick ad-hoc "dirty" test example:

import ttk
from Tkinter import *

class myTestFrame(Frame):

    def __init__(self):

        Frame.__init__(self)

        self.master.title("My Test Frame")

        self.master.minsize(350, 150)
        self.grid(sticky=W+N+S+E)

        firstLayer      = Frame(self)
        firstLayer.pack(side="top", fill="x")
        secondLayer      = Frame(self)
        secondLayer.pack(side="top", fill="x")
        thirdLayer      = Frame(self)
        thirdLayer.pack(side="top", fill="x")

        labelText=StringVar()
        labelText.set("Enter your area zip code: ")
        labelDir=Label(firstLayer, textvariable=labelText, fg="black", font = "Calibri 10 bold")
        labelDir.grid(row=2, column=0, sticky="W")
        zipCode=IntVar(None)
        entryFieldFrame=Entry(firstLayer,textvariable=zipCode,width=5)
        entryFieldFrame.grid(row=2, column=1, sticky="W", padx=31)

        ttk.Separator(secondLayer, orient='horizontal').grid(column=0,
            row=0, columnspan=2, sticky='ew')

        labelText=StringVar()
        labelText.set("Enter your age: ")
        labelDir=Label(secondLayer, textvariable=labelText, fg="black", font = "Calibri 10 bold")
        labelDir.grid(row=2, column=0, sticky="W")
        age=IntVar(None)
        age.set(1.0)
        entryFieldFrame=Entry(secondLayer,textvariable=age,width=5)
        entryFieldFrame.grid(row=2, column=1, sticky="W", padx=83)

        ttk.Separator(thirdLayer, orient='horizontal').grid(column=0,
            row=0, columnspan=2, sticky='ew')

        labelText=StringVar()
        labelText.set("Enter your brother's age: ")
        labelDir=Label(thirdLayer, textvariable=labelText, fg="black", font = "Calibri 10 bold")
        labelDir.grid(row=2, column=0, sticky="W")
        brothersAge=IntVar(None)
        entryFieldFrame=Entry(thirdLayer,textvariable=brothersAge,width=5)
        entryFieldFrame.grid(row=2, column=1, sticky="W", padx=29)

if __name__ == "__main__":

    testFrame = myTestFrame()
    testFrame.mainloop()
1
columnspan doesn't affect the width per se, it simply defines how many columns the widget spans. The actual width depends on how big the columns are. - Bryan Oakley
is it then somehow possible to explicitly (re)define the width of the separator, since I have different columns with different lenghts, but still do want to have separators with the SAME width/length? It seems that the padx option within the grid specification fails to help too. - schmi
yes and no. There are certainly tricks you can do such as putting the separator in a frame and then setting the size of the frame, but that's rarely required. Separators are designed to separate items, and are typically the same width or height of the things they are separating. It seems that you're using them for some other purpose, but there's not enough information in your question to suggest an alternative. Please give an mcve (stackoverflow.com/help/mcve) to illustrate how you're wanting fixed-width separators with variable width columns. - Bryan Oakley
well my setup is a rather simple one - I have raws of labels and entry fields (each raw contains one label and an entry field). The labels in each raw have different length/width - so want I do want is to group some of these rows with the help of Separators (that would hopefully have the same length) and not using frames - is that possible or I have to use Frames? - schmi
please provide an mcve. What you want to do is almost certainly possible, but what you want to do isn't very clear. - Bryan Oakley

1 Answers

3
votes

Your problem is not that the separators are too short, it's that you haven't told grid what to do with extra un-allocated space. Your columns are only exactly as wide as they need to be, and your separator is only as wide as the columns it is in. In a comment you say "I want them to go till the end of the entry fields" and that's exactly what they are doing. What you really want is for all of the entry fields to end at the same location.

The quick fix is to make sure that when you use grid, you always give at least one row and one column a non-zero weight. This tells grid where to allocate any extra space. In your case you haven't done this, so in the third row there is space that does unused to the right of the entry widget.

The quick and dirty solution here is to make sure that either column 0 or 1 gets the extra space. Usually the choice is to give it to the input widget. So, you can add this to improve the situation:

thirdLayer.grid_columnconfigure(1, weight=1)

This solves the immediate problem, but you need to do the same thing for every one of your frames, as well as for the root window. For every frame that contains children managed by grid, you need to give at least one row and one column a weight.

Since you seem to be trying to create a grid of labels and entry widgets, you might want to consider using a single frame rather than multiple frames. By using a single frame you don't have to try to guess how much padding to use to get everything to line up.