问题
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:
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.
来源:https://stackoverflow.com/questions/37924785/ttk-separator-set-the-length-width