簡體   English   中英

如何在 ttk.OptionMenu 周圍制作邊框

[英]How to make border around ttk.OptionMenu

在嘗試制作入口框架時,我遇到了一個問題,我無法在 ttk.OptionMenu 周圍制作邊框以使其看起來類似於 ttk.Entry。 (圖中兩人相鄰)

制作OptionMenu

option = ttk.OptionMenu(bottom_container, self.have, 'ANY', 'ANY', '0', '1', style='vista.TMenubutton')
option.grid(column=1, row=2, sticky='we')

我嘗試使用樣式(仍然希望使用 vista/winnative 外觀)並且能夠將選項菜單背景設為白色,但是我找不到適合它周圍邊框的方法

條目旁邊的選項菜單

這是我用於檢查 ttk 小部件的代碼,以便確定如何為它們設置主題:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
var = tk.StringVar()
widget = ttk.OptionMenu(root, var, 'ANY', 'ANY', '0', '1')
widget.grid(column=2, row=1, sticky='nesw')


style = widget.winfo_class()

s = ttk.Style()
#s.theme_use('clam')
elements = s.layout(style)

def get_element_details(elem, _dict, depth=1):
    print('%selement: %s' % (''.join(['\t' for s in range(depth)]), elem))
    for key in _dict:
        if key != 'children':
            print('%s%s: %s' % (''.join(['\t' for s in range(depth+1)]), key, _dict[key]))
    print('%soption: %s' % (''.join(['\t' for s in range(depth+1)]), s.element_options(elem)))
    if 'children' in _dict:
        for child, child_dict in _dict['children']:
            get_element_details(child, child_dict, depth+1)

print('element: %s' % style)
print('option: %s' % str(s.element_options(style)))
for elem, elem_dict in elements:
    get_element_details(elem, elem_dict)
root.mainloop()

簡單地說,沒有將主題切換為蛤蜊主題之類的東西,小部件中沒有任何選項可以添加邊框(愚蠢的我知道)

我沒有要測試的 vista 主題,但是使用 XP 主題我得到:

element: TMenubutton
option: ()
    element: Menubutton.dropdown
        side: right
        sticky: ns
        option: ()
    element: Menubutton.button
        expand: 1
        sticky: nswe
        option: ()
        element: Menubutton.padding
            expand: 1
            sticky: we
            option: ('-padding', '-relief', '-shiftrelief')
            element: Menubutton.label
                sticky: 
                option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')

和蛤蜊主題:

element: TMenubutton
option: ()
    element: Menubutton.border
        sticky: nswe
        option: ('-bordercolor', '-lightcolor', '-darkcolor', '-relief', '-borderwidth')
        element: Menubutton.focus
            sticky: nswe
            option: ('-focuscolor', '-focusthickness')
            element: Menubutton.indicator
                sticky: 
                side: right
                option: ('-arrowsize', '-arrowcolor', '-arrowpadding')
            element: Menubutton.padding
                expand: 1
                sticky: we
                option: ('-padding', '-relief', '-shiftrelief')
                element: Menubutton.label
                    sticky: 
                    side: left
                    option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')

注意到添加了邊框元素嗎?

如果您嘗試使用以下方法將蛤蜊主題選項菜單的布局復制到另一個主題:

newlayout = [('Menubutton.border', {'children': [('Menubutton.focus', {'children': [('Menubutton.indicator', {'sticky': '', 'side': 'right'}), ('Menubutton.padding', {'sticky': 'we', 'expand': '1', 'children': [('Menubutton.label', {'sticky': '', 'side': 'left'})]})], 'sticky': 'nswe'})], 'sticky': 'nswe'})]
s.layout(style, newlayout)

結果是:

element: TMenubutton
option: ()
    element: Menubutton.border
        sticky: nswe
        option: ('-relief',)
        element: Menubutton.focus
            sticky: nswe
            option: ()
            element: Menubutton.indicator
                sticky: 
                side: right
                option: ('-direction', '-arrowsize', '-arrowcolor')
            element: Menubutton.padding
                sticky: we
                expand: 1
                option: ('-padding', '-relief', '-shiftrelief')
                element: Menubutton.label
                    sticky: 
                    side: left
                    option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')

它不再具有實際設置邊框的選項。 本質上,主題引擎無法正確處理具有不同元素的布局。 所以你需要選擇一個主題,其中包含你想要設置樣式的所有元素的小部件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM