对所有 GUI 小部件应用一致的填充

时间:2021-02-14 23:36:00

标签: python tkinter ttk

我正在用 Python 构建一个更大的基于 tkinter 的 GUI,我试图保持外观,尤其是间距,在多个 list2env 之间保持一致,每个 library(stringr) deframe(out) %>% set_names(str_c('group', seq_along(.))) %>% list2env(.GlobalEnv) group1 # [,1] [,2] #[1,] 6 0 #[2,] 0 2 group2 # [,1] [,2] #[1,] 2 -1 #[2,] -1 2 group3 # [,1] [,2] #[1,] 6.8 2.6 #[2,] 2.6 5.2 都包含多个按钮、分隔符等。

目前我正在使用递归方法来实现这一目标,大致如下:

ttk.LabelFrame

但似乎改变 ttk 样式是实现相同目标的更优雅的选择。然而,虽然这种方法适用于按钮等一些小部件:

def adjust_children(parent):
    for child in parent.winfo_children():
        if isinstance(child, ttk.Labelframe):
            child.grid_configure(padx=10, pady=10, ipadx=5, ipady=5, sticky='senw')
            adjust_children(child)
        elif isinstance(child, ttk.Frame):
            child.grid_configure(padx=0, pady=0, ipadx=0, ipady=0, sticky='senw')
            adjust_children(child)
        elif isinstance(child, ttk.Separator):
            child.grid_configure(padx=4, pady=4)
        else:
            child.grid_configure(padx=2, pady=2)

以上似乎不适用于 style = ttk.Style() style.theme_settings('default', { 'TButton':{'configure': {'padding': (2, 2)}}, }) 。同样,我找不到太多有关使用样式更改“内部”填充(上面的TSeparatoripadx)的信息。

我是否遗漏了某些东西,或者我是否可以使用对默认 ttk 样式的更改来将填充应用于分隔符?有没有比上面显示的递归方法更好的方法?

1 个答案:

答案 0 :(得分:0)

如果你不打算使用Style,那么你可以用这种方式代替递归

def adjust_children (parent) :
    _list=parent.winfo_children()
    for item in _list:
        for widget in list(attrs.keys())[:-1]:
            if isinstance(item,widget):
                item.grid_configure(**attrs[widget])
                break
        else:
            item.grid_configure(**attrs['None'])
        if item.winfo_children():
            _list.extend(item.winfo_children())

attrs={
    ttk.Labelframe: {'padx':10, 'pady':10, 'ipadx':5, 'ipady':5, 'sticky':'senw'},
    ttk.Frame: {'padx':0, 'pady':0, 'ipadx':0, 'ipady':0, 'sticky':'senw'},
    ttk.Separator: {'padx':4, 'pady':4},
    'None': {'padx':2, 'pady':2},
}
相关问题