0

I'm not able to correctly align ttk widgets in the simple following application.

enter image description here

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. Desirable situation

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:

enter image description here

4

2 回答 2

0

组合框不在导入按钮右侧的原因是因为第 0 列比您想象的要宽。如果您暂时注释掉,您可以看到组合框正对着按钮F1.grid(row=1,column=0,columnspan=2)

之所以这么宽,是因为您放入F1第 0 列并让它跨越两列,因此根据定义,第 0 列和第 1 列加起来必须与F1.

一个简单的解决方案是让第三列是空的,并让它占用额外的空间。通过F1跨越三列并为第三列赋予非零权重来做到这一点。然后该列将获得所有额外空间,因此第 0 列和第 1 列将仅与该列中最大的项目一样大。

复选按钮的问题很简单,无论您是否有标签,标签都有空间,因此实际的复选框向左偏移。由于您使用的是 ttk Checkbutton,因此如果您希望选中按钮居中,则可以从该小部件的样式布局中完全删除标签。

它应该看起来像这样:

ttk.Style().layout("CustomCheckbutton", [('Checkbutton.button', {'sticky': 'nswe'})])
...
ttk.Checkbutton(F1, style="CustomCheckbutton").grid(row=1,column=3) # Crops
ttk.Checkbutton(F1, style="CustomCheckbutton").grid(row=1,column=4)  # Show
于 2020-12-22T17:25:57.947 回答
0

最后,解决方案(尽管不是 100% 满意)是将第一行封装到一个框架中,并使用 tk.Checkbutton() 而不是 ttk.Checkbutton() 复选框。

F0 = ttk.Frame(self.root)       
ttk.Button(F0,text='Import').grid(row=0,column=0,padx=5,sticky="w")
ttk.Combobox(F0,.....).grid(row=0,column=1,sticky="w") 
F0.grid(row=0,column=0,sticky="w")

并为 Checkbutton 使用 tk 而不是 tkk 小部件

# ttk.Checkbutton(F1).grid(row=1,column=3,sticky='we')
tk.Checkbutton(F1).grid(row=1,column=3,sticky='we')

在我看来很奇怪 ttk 没有正确遵守“粘性”行为(也许是一个错误?)。

于 2020-12-22T15:11:07.910 回答