I'm not able to correctly align ttk
widgets in the simple following application.
Code:
from tkinter import *
from tkinter import ttk
class App:
def __init__(self, root):
self.frames = []
self.entries = []
self.root = root
self.root.title("Trajectories")
self.FOPTIONS = ['A','B','C','D']
self.LSOURCE = StringVar()
self.LSOURCE.set(self.FOPTIONS[0])
self.FINDX = IntVar()
self.FLIST = ['0']
ttk.Style().configure("TFrame", padding=6, relief="flat",background="#ccc")
#F0 = ttk.Frame(self.root,padding=5)
ttk.Button(self.root,text='Import').grid(row=0,column=0,padx=5,sticky="w")
ttk.Combobox(self.root,textvariable=self.LSOURCE,values=self.FOPTIONS).grid(row=0,column=1,sticky="w")
F1 = ttk.Frame(self.root,padding=5)
# Labels row
ttk.Label(F1,text='Number').grid(row=0,column=0)
ttk.Label(F1,text='File').grid(row=0,column=1)
ttk.Label(F1,text='Alias').grid(row=0,column=2)
ttk.Label(F1,text='Crop').grid(row=0,column=3)
ttk.Label(F1,text='Show').grid(row=0,column=4)
# Fields row
ttk.Combobox(F1,textvariable=self.FINDX,values=self.FLIST,width=5).grid(row=1,column=0) # File
ttk.Entry(F1,width=50).grid(row=1,column=1,padx=3,sticky='w')
ttk.Entry(F1,width=15).grid(row=1,column=2,sticky='w') # Alias
ttk.Checkbutton(F1).grid(row=1,column=3,sticky='we') # Crops
ttk.Checkbutton(F1).grid(row=1,column=4,sticky='we') # Show
F1.grid(row=1,column=0,columnspan=2)
Button(self.root, text="Create",command=self.draw).grid(row=2,column=0,columnspan=2)
def draw(self):
print("Draw pressed")
root = Tk()
App(root)
root.mainloop()
The troubles are:
(i) with the first ttk.Combobox()
and
(ii) Checkbuttons()
under "Crop" and "Show" labels. I have tried many strategies but:
I haven't been able to place the
ttk.Combobox()
completely aligned to the left just close to the button "Import".Checkbuttons()
are without text and are not centered with respect to the labels in the row above.
The desirable situation should be something similar to the graphic below.
I have tried some variant encapsulating the first row into a frame but nothing changes. The second row with labels and the third row with the fields are encapsulated within a frame just to group a variable number of additional rows. the changes using sticky values did not produce any effect. Just I don't understand in this example how grid()
is working in such a way and why the Combobox
does not justify to the left("w") and why the "ew"
does not center Checkbuttons()
inside the cells.
If I try to grid the "Import" button as (row=0,column=0,sticky="w") an the ttk.Combobox().grid(row=0,column=0,sticky="w") as suggested in one of the comments I get: