Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

python - ttk.Separator set the length/width

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...